0

I have a big text file(with 15 lines) in this format (srip dip sport dport .....) I would like to group distinct first column and get the group counts,

input file:

10.10.10.1 10.10.66.150 60761 502 2019-02-28 13:30:31.447 0 6 ....S. 0 1 44 1 cluster1
10.10.10.1 10.10.66.150 60761 502 2019-02-28 13:30:31.447 0 6 ....S. 0 1 44 1 cluster1
10.10.10.3 10.10.66.150 60761 502 2019-02-28 13:30:31.447 0 6 ....S. 0 1 44 1 cluster1
10.10.10.4 10.10.66.150 60761 502 2019-02-28 13:30:31.447 0 6 ....S. 0 1 44 1 cluster1

I want to get output file sort by counts like as below,

10.10.10.1 2
10.10.10.3 1
10.10.10.4 1

How can I do this?

0

This code snippet can be used to serve the desired purpose

use strict
use warnings

my $filename = 'data.txt';
open(my $fh, '<', $filename)  or die "Could not open file '$filename' $!";
my %count;
while (my $line = <$fh>) {
    my @words = split(' ', $line);
    my $ip = $words[0];
    $count{$ip}++;
}

while (my ($k,$v)=each %count){
    print "$k $v\n"
}

Explanation

  • Open the file for reading
my $filename = 'data.txt';
open(my $fh, '<', $filename)  or die "Could not open file '$filename' $!";
  • Iterate over lines of file and find words by splitting the line on space. Extract the first column value from each line and store it in a hash, with the column as key and value as number of occurrence of key
my %count;
while (my $line = <$fh>) {
    my @words = split(' ', $line);
    my $ip = $words[0];
    $count{$ip}++;
}

Note hash has following structure { "key" : "value" } Here hash will contain the values as: { '10.10.10.1' => 2, '10.10.10.3' => 1, '10.10.10.4' => 1 }

  • Finally iterate over key, value pairs in count hash and print the key i.e column and value i.e count
foreach my $key (sort { $count{$b} <=> $count{$a} } keys %count) {
    print "$key $count{$key}\n"
}
  • Here sort { $count{$b} <=> $count{$a} } is for descending order sort on hash values
  • Use sort { $count{$a} <=> $count{$b} } for ascending order sort on hash values
  • Use sort { $a <=> $b } for ascending order sort on hash keys
  • Use sort {$b <=> $a} for descending order sort on hash keys
  • how to sort by count ? – georgetovrea Jun 24 at 5:36
  • I have edited my answer and added a sort function on values for descending order – Arushix Jun 24 at 5:44
  • thanks, but it's only print counts sort results ,didnt print ip ? It's possible to print ip and count and sort by count ? – georgetovrea Jun 24 at 6:07
  • It must be printing both ip and count print "$key $count{$key}\n", here $key is ip and $count{$key} is count, can you check again ? – Arushix Jun 24 at 6:09
  • Can you paste the output as well, what it is returning – Arushix Jun 24 at 6:43
0

I suggest simplicity:

cat [*filename*] | cut -f 1 -d \“ \“ | sort -n | uniq -c

F.

  • This doesn’t count the occurrences... Please avoid smart quotes in code too, it means readers can’t copy and paste. – Stephen Kitt Jun 24 at 12:13
  • Alas, did I forget the ‘-c’ appendage? No worries, I edit my answer... – Francis from ResponseBase Jun 24 at 20:12
  • Your quotes are still messed up ;-) – Stephen Kitt Jun 24 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.