awk to grab the most found lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to grab the most found lines
# 1  
Old 02-24-2013
awk to grab the most found lines

Code:
awk 'NR>=5034&&/Max/{++c}c==3{o=$0 RS $0 RS $0; print o; c=0}' logfile

i would like to modify the above awk command to instead show lines that are the most frequent:

for instance, given a log file containing:

Code:
Warning MaxClient is having issues - please
MaxClients java error...yams potato turkey
MaxClients error Fatal exception known known
MaxClients error Fatal exception known known
Could not complete. Error found. MaxClient initiated
MaxClients error Fatal exception known known
Could not complete. Error found. MaxClient initiated

I want the awk statement to show only the lines that occur the most. The content of the log should not matter. if I egrep for everything in the log, how do i modify the awk statement to show the lines that are the most frequent?

Code:
awk 'NR>=5034&&/./{++c}c==3{o=$0 RS $0 RS $0; print o; c=0}' logfile

# 2  
Old 02-24-2013
Try:
Code:
sort logfile | uniq -c | sort -nrk1 | head

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 02-24-2013
Code:
awk 'NR>=5034{a[$0]++}END{for(i in a) { if(a[i]>max) { max=a[i]; v=i } } print v }' logfile

This User Gave Thanks to Yoda For This Post:
# 4  
Old 02-24-2013
Quote:
Originally Posted by bipinajith
Code:
awk 'NR>=5034{a[$0]++}END{for(i in a) { if(a[i]>max) { max=a[i]; v=i } } print v }' logfile

can you please explain this statement?
# 5  
Old 02-24-2013
Code:
awk ' NR >= 5034 {
        a[$0]++                         # Store current record in array and count occurrances
}
END {                                   # END block
        for (i in a) {
                if (a[i] > max) {       # If a[i] ( number of occurrances ) > max
                        max = a[i]      # Set max = a[i] ( number of occurances )
                        v = i           # Set v = i ( array element )
                }
        }
        print v                         # Print v
}' file

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed/awk join lines once pattern found

Hi all OS - RHEL6.4 I have input file -f1.txt I need to search line which starts with \Start and read next line till it gets blank line and join them all. I need to trim any trailing spaces for each line.So output.txt should be.. \Start\now\fine stepwatch this space for toolsends... (7 Replies)
Discussion started by: krsnadasa
7 Replies

2. Shell Programming and Scripting

Grab fields without awk

so i want to avoid external calls in my script, so im trying to do the following: AllMyLogs="hello---nallo---wello---bollo tello---zello---jello---kello" OLDIFS="$IFS\\\n" IFS="---" set -- ${AllMyLogs} IFS="$OLDIFS" I want it to work in a way that, when i type this: echo $2 I... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Shell Programming and Scripting

awk remove/grab lines from file with pattern from other file

Sorry for the weird title but i have the following problem. We have several files which have between 10000 and about 500000 lines in them. From these files we want to remove lines which contain a pattern which is located in another file (around 20000 lines, all EAN codes). We also want to get... (28 Replies)
Discussion started by: SDohmen
28 Replies

4. UNIX for Dummies Questions & Answers

awk - Print lines if only matching key is found

I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. Thanks a lot. Any help is appreciated. Script I am using: awk 'FNR == NR && ! /^]*$/ {... (9 Replies)
Discussion started by: High-T
9 Replies

5. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

6. Shell Programming and Scripting

Cant get awk 1liner to remove duplicate lines from Delimited file, get "event not found" error..help

Hi, I am on a Solaris8 machine If someone can help me with adjusting this awk 1 liner (turning it into a real awkscript) to get by this "event not found error" ...or Present Perl solution code that works for Perl5.8 in the csh shell ...that would be great. ****************** ... (3 Replies)
Discussion started by: andy b
3 Replies

7. Shell Programming and Scripting

awk to print all lines after a pattern is found

Is there a way with aw to print all lines after a string is found There is a file like this ....... ........ 2012/19/11 :11.58 PM some data lne no date 2012/19/11 :11.59 PM some other data 2012/20/11 :12.00 AM some other data some line without dates some more lines without dates... (8 Replies)
Discussion started by: swayam123
8 Replies

8. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

9. Shell Programming and Scripting

script to grab lines between two values

hi guys say I have a file that contains hello world this is AAAAA message to try BBBBBB and see if anyone AAAAA knows the (3 Replies)
Discussion started by: JamesByars
3 Replies

10. Shell Programming and Scripting

grab next consecutive line or lines

Hello i'm writting a krn shell that will find the letter F and I would like to grab everything from F till 0:00. Is there a sed or awk command that i could use. Thank you. File is looks like this. 11/12 xxx xxxx xxx F xxxx xxxx some info entered here some info entered here xxxx... (1 Reply)
Discussion started by: wisher115
1 Replies
Login or Register to Ask a Question