我有一个看起来像这样的文件:
192.168.2.2 150.25.45.7 8080
192.168.12.25 178.25.45.7 50
192.168.2.2 142.55.45.18 369
192.168.489.2 122.25.35.7 8080
192.168.489.2 90.254.45.7 80
192.168.2.2 142.55.45.18 457
我编了所有数字。
我需要根据第一个ip的重复次数对所有这些文件进行排序。所以输出理想情况如下:
192.168.2.2 8080 369 457 3
192.168.489.2 8080 80 2
192.168.12.25 50 1
所以:首先是ip,所有与第一个ip一致的所有端口,以及重复次数。
我一直在尝试使用sort命令和awk,但我不想做额外的工作,也许会错过其他一些简单的解决方案。
有什么想法吗?谢谢:))
答案 0 :(得分:7)
Perlish的答案看起来像这样。
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
my %data;
# Store IP address and port number
while (<DATA>) {
chomp;
my ($ip, undef, $port) = split;
push @{$data{$ip}}, $port;
}
# Sort (in reverse) by length of list of ports
for (sort { @{$data{$b}} <=> @{$data{$a}} } keys %data) {
say "$_ @{$data{$_}} ", scalar @{$data{$_}};
}
__DATA__
192.168.2.2 150.25.45.7 8080
192.168.12.25 178.25.45.7 50
192.168.2.2 142.55.45.18 369
192.168.489.2 122.25.35.7 8080
192.168.489.2 90.254.45.7 80
192.168.2.2 142.55.45.18 457
输出:
192.168.2.2 8080 369 457 3
192.168.489.2 8080 80 2
192.168.12.25 50 1
答案 1 :(得分:2)
Perl方式:
#!/usr/bin/perl
use strict;
use warnings;
my %repeat;
while(<DATA>) {
if (/^(\d+(?:.\d+){3})\s\S+\s(\d+)$/) {
push @{$repeat{$1}}, $2;
}
}
foreach (sort {@{$repeat{$b}}<=>@{$repeat{$a}}} keys %repeat) {
my $num = @{$repeat{$_}};
print "$_ @{$repeat{$_}} $num\n";
}
__DATA__
192.168.2.2 150.25.45.7 8080
192.168.12.25 178.25.45.7 50
192.168.2.2 142.55.45.18 369
192.168.489.2 122.25.35.7 8080
192.168.489.2 90.254.45.7 80
192.168.2.2 142.55.45.18 457
<强>输出:强>
192.168.2.2 8080 369 457 3
192.168.489.2 8080 80 2
192.168.12.25 50 1
答案 2 :(得分:1)
另一个Perl解决方案:
#!/usr/bin/perl
use strict;
use warnings;
my %ips;
push @{$ips{$_->[0]}}, $_->[1]+0 for map{[split/ \S+ /]}<DATA>;
for (sort {@{$ips{$b}} <=> @{$ips{$a}}} keys %ips) {
printf("%s %s %d\n", $_, join(" ", @{$ips{$_}}), 0+@{$ips{$_}});
}
__DATA__
192.168.2.2 150.25.45.7 8080
192.168.12.25 178.25.45.7 50
192.168.2.2 142.55.45.18 369
192.168.489.2 122.25.35.7 8080
192.168.489.2 90.254.45.7 80
192.168.2.2 142.55.45.18 457
输出:
192.168.2.2 8080 369 457 3
192.168.489.2 8080 80 2
192.168.12.25 50 1
答案 3 :(得分:0)
这一行应该为你完成这项工作:
awk '{a[$1]++;b[$1]=b[$1]" "$3}END{for(x in a)print a[x]"\t"x,b[x],a[x]}' input |
sort -nr|cut -f2-
您的示例 测试
kent$ cat tt
192.168.2.2 150.25.45.7 8080
192.168.12.25 178.25.45.7 50
192.168.2.2 142.55.45.18 369
192.168.489.2 122.25.35.7 8080
192.168.489.2 90.254.45.7 80
192.168.2.2 142.55.45.18 457
kent$ awk '{a[$1]++;b[$1]=b[$1]" "$3}END{for(x in a)print a[x]"\t"x,b[x],a[x]}' tt |
sort -nr|cut -f2-
192.168.2.2 8080 369 457 3
192.168.489.2 8080 80 2
192.168.12.25 50 1
答案 4 :(得分:0)
这是一个主要依赖于awk和sort的管道:
sort -k1 -k3n \
| awk -F' ' '
NR==1 {
printf("%s ", $1);
current = $1
}
$1 != current {
printf(":%d\n%s ", count, $1);
current = $1;
count = 0
}
{ printf("%d ", $3); count++ }
END { printf(":%d\n", count) }' \
| sort -t':' -k2nr \
| tr -d':'
答案 5 :(得分:0)
GNU awk 4:
awk 'END {
PROCINFO["sorted_in"] = "@val_num_desc"
for (e in ic)
print e, ip[e], ic[e]
}
{
ip[$1] = $1 in ip ? ip[$1] OFS $NF : $NF
ic[$1]++
}' infile