Solved

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

Posted on 2008-10-20
5
645 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

810 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