Solved

How can I grep, parse lines, and return count by certain fields?

Posted on 2008-10-20
5
643 Views
Last Modified: 2013-12-26
-need a one liner that greps a file for all entries with 'FAIL'
-then it will parse through the lines and return a count for each unique name in field 'd' (note that fields are not always in the same order)
-if field d does not exist, count by field 'g'
Example:
XXXXXXXXXXXXXXXXXXXXXX$a=1234:d=fred:y=alphazXXXXXXXXXXXmFAIL
XXXXXXXXXXXXXXXXXXXXXX$d=fred:a=4321:y=bravozXXXXXXXXXXXmFAIL
XXXXXXXXXXXXXXXXXXXXXX$a=1234:g=jackson:y=charliezXXXXXXXXXXXmFAIL
XXXXXXXXXXXXXXXXXXXXXX$y=delta:a=1357:d=bobbyzXXXXXXXXXXXmFAIL
--------------------------------
If you were to run the one-liner on the above lines, the output should be:
fred 2
jackson 1
bobby 1

-If need be, it is ok to call perl for this execution
0
Comment
Question by:uaMozilla
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:tvedtem
ID: 22760279
can you give me the XXXXX values (or are they really XXXX) ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 22760445
perl -ne '/FAIL/ && (/\bd=(\w+)/||/\bg=(\w+)/) && $c{$1}++;END{print"$_ $c{$_}\n" for keys %c}'  file
0
 

Author Comment

by:uaMozilla
ID: 22760870
Ozo,
  Is there someway to put a zgrep or something in front of all this so that it works with gzipped files?
Thanks
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 22761012
zgrep FAIL file | perl -ne '(/\bd=(\w+)/||/\bg=(\w+)/) && $c{$1}++;END{print"$_ $c{$_}\n" for keys %c}'  
0
 

Author Closing Comment

by:uaMozilla
ID: 31507922
Perfect. Thanks Ozo.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now