chitech
November 12, 2010, 2:32pm
1
Alo
I have a file with a lot of addresses where I want to list unique addresses and the number of theirs occurrence.
I have this input file:
0011bd09 ea 01 0b 04 ea 01 0b 38-bd 11 00 98 15 cb 01 00 .......8........
0011bd09 ea 11 00 98 15 cb 01 00-00 00 00 d8 3d 8d 01 94 ............=...
0011bd0d ea 01 0b 38 bd 11 00 98-15 cb 01 00 00 00 00 d8 ...8............
0011bd21 ea 01 0b 00 00 00 00 00-00 00 00 e9 2a f5 27 54 ............*.'T
0011e9f9 ea 11 00 98 15 cb 01 00-00 00 00 d8 3d 8d 01 94 ............=...
0011ea21 ea 11 00 98 15 cb 01 00-00 00 00 d8 3d 8d 01 e4 ............=...
And I want something like this (order by number of occurrence):
0011bd09 2
0011bd0d 1
0011bd21 1
0011e9f9 1
0011ea21 1
I am thinking about using uniq combined with sort but I only want to uniq on the address and not the hole line
Thx in advance
Hope this helps:
awk '{print $1}' inputfile | uniq -c | awk '{print $2" "$1}'
1 Like
chitech
November 12, 2010, 3:32pm
3
Thx for the answer but it's not working correctly. All lines get 1 occurrence. But if I use this:
awk '{print $1}' inpufFile | sort | uniq -c | sort | awk '{print $2" "$1}'
I will get the right result. The first sort is to make uniq -c count correctly and the second sort is to sort the occurrence
No problem. Interesting. On my distro (Arch Linux), it yielded the correct output without the 'sorts', but the bottomline is you got the answer you were looking for.
Here's a Perl solution -
$
$
$ # show the content of the data file "f7"
$
$ cat f7
0011bd09 ea 01 0b 04 ea 01 0b 38-bd 11 00 98 15 cb 01 00 .......8........
0011bd09 ea 11 00 98 15 cb 01 00-00 00 00 d8 3d 8d 01 94 ............=...
0011bd0d ea 01 0b 38 bd 11 00 98-15 cb 01 00 00 00 00 d8 ...8............
0011bd21 ea 01 0b 00 00 00 00 00-00 00 00 e9 2a f5 27 54 ............*.'T
0011e9f9 ea 11 00 98 15 cb 01 00-00 00 00 d8 3d 8d 01 94 ............=...
0011ea21 ea 11 00 98 15 cb 01 00-00 00 00 d8 3d 8d 01 e4 ............=...
0011bd0d xx 01 0b 38 bd 11 00 98-15 cb 01 00 00 00 00 d8 ...8............
0011bd0d yy 01 0b 38 bd 11 00 98-15 cb 01 00 00 00 00 d8 ...8............
$
$ # run the Perl script on "f7"
$
$ perl -lane '$x{$F[0]}++;
END {
while (($k, $v) = each %x) {push @y,"$k:$v"}
@z = sort {(split ":",$b)[1] <=> (split ":",$a)[1]} @y;
do {s/:/ /; print} for (@z)}
' f7
0011bd0d 3
0011bd09 2
0011ea21 1
0011bd21 1
0011e9f9 1
$
$
tyler_durden
danmero
November 12, 2010, 4:55pm
6
Only awk and sort.
awk '{a[$1]++}END{for(i in a) print i,a|"sort -rk2"}' file