Link to home
Start Free TrialLog in
Avatar of uaMozilla
uaMozilla

asked on

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

-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
Avatar of tvedtem
tvedtem
Flag of United States of America image

can you give me the XXXXX values (or are they really XXXX) ?
Avatar of ozo
perl -ne '/FAIL/ && (/\bd=(\w+)/||/\bg=(\w+)/) && $c{$1}++;END{print"$_ $c{$_}\n" for keys %c}'  file
Avatar of uaMozilla
uaMozilla

ASKER

Ozo,
  Is there someway to put a zgrep or something in front of all this so that it works with gzipped files?
Thanks
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect. Thanks Ozo.