find matches in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find matches in file
# 1  
Old 10-08-2009
Java find matches in file

Hi, im have log file ~100000 lines,

Code:
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

Code:
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!

Last edited by Trump; 10-08-2009 at 10:24 AM..
# 2  
Old 10-08-2009
Code:
awk '{a[$1]++}END{for(i in a)print i" matches "a[i]}' 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 [code] and [/code] 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
# 3  
Old 10-08-2009
Code:
# 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;

Code:
$ 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

# 4  
Old 10-08-2009
thanx, but i need to sort it by matches increase , or top 10 matches.
# 5  
Old 10-08-2009
Code:
awk '{a[$1]++}END{for(i in a)print i" matches "a[i]}' infile |sort -k3,3nr | head -10

Jean-Pierre.

Last edited by aigles; 10-08-2009 at 11:42 AM.. Reason: Add miising input file for awk
# 6  
Old 10-08-2009
And another one in Perl:

all

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

top 10

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

# 7  
Old 10-08-2009
Quote:
Originally Posted by aigles
Code:
awk '{a[$1]++}END{for(i in a)print i" matches "a[i]}'|sort -k3,3nr | head -10

Jean-Pierre.
I think this needs an infile

Like the indexing of the array, very nice
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a text and if condition matches then replace it

Need a script that can find text in a file and replace it accordingly. This is the file I have: while IFS=',' read -r f1 f2 f3 do { nohup /home/testuser/dbmaintenance/sys_offline_maintenance.sh $f1 $f2 $f3 > $f2.out & } done < "/home/testuser/dbmaintenance/week1offlineserver.txt" In... (4 Replies)
Discussion started by: singhhe
4 Replies

2. UNIX for Beginners Questions & Answers

find pattern matches in consecutive lines in certain fields-awk

I have a text file with many thousands of lines, a small sample of which looks like this: InputFile:PS002,003 D -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0 PS002,003 PSQ 0 1 7 18 1 0 -1 1 1 3 -1 -1 ... (5 Replies)
Discussion started by: jvoot
5 Replies

3. Shell Programming and Scripting

Required 3 lines above the file and below file when string matches

i had requirement like i need to get "error" line of above 3 and below 3 from a file .I tried with the below script.But it's not working. y='grep -n -i error /home/file.txt|cut -c1' echo $y head -$y /home/file.txt| tail -3 >tmp.txt tail -$y /home/file.txt head -3 >>tmp.txt (4 Replies)
Discussion started by: bhas85
4 Replies

4. Shell Programming and Scripting

Find matches and write the data before it

Hi all I am here for help once again I have two files One file is like this with one columns F2 B2 CAD KGM HTC CSP Second file is like this in 5 columns where firs column contain sometime entries of first file with space and other entries (12 Replies)
Discussion started by: Priyanka Chopra
12 Replies

5. Shell Programming and Scripting

Find file that matches today's date in filename and move to /tmp in bash

I'm having problems with my bash script. I would like to find a file matching today's date in the filename, i.e. my_file_20120902.txt and then move it to a different directory, i.e. /tmp. Thanks. (1 Reply)
Discussion started by: jamesi
1 Replies

6. Shell Programming and Scripting

zgrep cannot find all matches

$ cat 1.csv abc in csv $ cat 1.mnf abc in mnf $ zip 1.zip 1.csv 1.mnf adding: 1.csv (stored 0%) adding: 1.mnf (stored 0%) $ zgrep abc 1.zip abc in csv How come zgrep cannot find "abc in mnf"? Thanks in advance. (5 Replies)
Discussion started by: carloszhang
5 Replies

7. UNIX for Dummies Questions & Answers

Pipe binary file matches grep results to file

I am using grep to match a pattern, but the output is strange. $ grep -r -o "pattern" * Gives me: Binary file foo1 matches Binary file foo2 matches Binary file foo3 matches To find the lines before/after, I then have to use the following on each file: $ strings foo1 | grep -A1 -B1... (0 Replies)
Discussion started by: chipperuga
0 Replies

8. Shell Programming and Scripting

Find directories that contains more than n matches of a certain filename

I need to construct a command that finds directories which contains more than n matches of a certain filename. E.g. I have many directories at different locations and want to find all directories that has 2 or more .dat-files. I thought of using find and maybe the exec parameter to issue an... (5 Replies)
Discussion started by: odyssey
5 Replies

9. Shell Programming and Scripting

find/grep returns no matches

Hi all! I've faced with very unintelligible error using find/grep like this: root@v29221:~# find /var/www/igor/data/www/lestnitsa.ru | grep u28507I get nothing as a result, but: root@v29221:~# grep u28507 /var/www/igor/data/www/lestnitsa.ru/_var.inc $db_name = 'u28507';... (2 Replies)
Discussion started by: ulrith
2 Replies

10. Shell Programming and Scripting

Need to find a string, check the next line, and if it matches certain criteria, replace it with a s

Hey Fellas. I am new to scripting. I have searched through the forums and found a lot of good info, but I can't seem to get any of it to work together. I am trying to find a particular sting in a file, and if the next string matches certain criteria, replace it with a string from a csv... (6 Replies)
Discussion started by: midniteslice
6 Replies
Login or Register to Ask a Question