find matches in file

Hi, im have log file ~100000 lines,

192.168.29.1 at 10/08/09 13:58:55
192.168.60.1 at 10/08/09 14:11:28
192.168.58.171 at 10/08/09 14:12:45
192.168.61.12 at 10/08/09 14:15:44
192.168.60.1 at 10/08/09 14:16:36
192.168.60.1 at 10/08/09 14:17:43
192.168.61.12 at 10/08/09 14:18:08
192.168.63.88 at 10/08/09 14:22:04
192.168.58.171 at 10/08/09 14:25:06
192.168.61.12 at 10/08/09 14:27:27

need to grep ip adress and find how many times this matches to list

192.168.60.1 matches 4
192.168.61.12 matches 3
192.168.58.171 matches 2
192.168.63.88 matches 1
etc...

thnx for reply!

awk '{a[$1]++}END{for(i in a)print i" matches "a}' file

To keep the forums high quality for all users, please take the time to format your posts correctly.

  1. Use Code Tags when you post any code or data samples so others can easily read your code.
    You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags and by hand.)
  2. Avoid adding color or different fonts and font size to your posts.
    Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
  3. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply With Quote

# put all the ip into a hash as key, and counts as value.
while(<>)  {
    $hash{$1}++ if /^([^ ]*)/;
}

# print the hash.
print "$_ matches $hash{$_}\n" foreach keys %hash;
$ cat sample-input
192.168.29.1 at 10/08/09 13:58:55
192.168.60.1 at 10/08/09 14:11:28
192.168.58.171 at 10/08/09 14:12:45
192.168.61.12 at 10/08/09 14:15:44
192.168.60.1 at 10/08/09 14:16:36
192.168.60.1 at 10/08/09 14:17:43
192.168.61.12 at 10/08/09 14:18:08
192.168.63.88 at 10/08/09 14:22:04
192.168.58.171 at 10/08/09 14:25:06
192.168.61.12 at 10/08/09 14:27:27
192.168.61.12 at 10/08/09 14:18:08



$ perl t.pl sample-input
192.168.61.12 matches 4
192.168.29.1 matches 1
192.168.63.88 matches 1
192.168.60.1 matches 3
192.168.58.171 matches 2

thanx, but i need to sort it by matches increase , or top 10 matches.

awk '{a[$1]++}END{for(i in a)print i" matches "a}' infile |sort -k3,3nr | head -10

Jean-Pierre.

And another one in Perl:

all

perl -ane'
    $_{ $F[0] }++;
    print map { $_, " n. of matches: ", $_{$_}, "\n" }
      sort { $_{$b} <=> $_{$a} } keys %_
      if eof
  ' infile

top 10

perl -ane'
    $_{ $F[0] }++;
    print map { $_, " n. of matches: ", $_{$_}, "\n" }
      ( sort { $_{$b} <=> $_{$a} } keys %_ )[ 0 .. 9 ]
      if eof
  ' infile

I think this needs an infile

Like the indexing of the array, very nice

slightly changed solution of "thegeek"

$ cat t.pl
# put all the ip into a hash as key, and counts as value.
while(<>)  {
    $hash{$1}++ if /^([^ ]*)/;
}
# print the hash.
map { print "$_ matches $hash{$_}\n" } sort {$hash{$a} <=> $hash{$b}} keys %hash;


$ perl t.pl sample
192.168.29.1 matches 1
192.168.63.88 matches 1
192.168.58.171 matches 2
192.168.60.1 matches 3
192.168.61.12 matches 4

awk '{a[$1]++}END{for(i in a)print i" matches "a}' infile |sort -k3,3nr | head -10

Jean-Pierre.

Big thanks for so many solutions :slight_smile: