Solved

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

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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