Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 650
  • Last Modified:

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
0
uaMozilla
Asked:
uaMozilla
  • 2
  • 2
1 Solution
 
tvedtemCommented:
can you give me the XXXXX values (or are they really XXXX) ?
0
 
ozoCommented:
perl -ne '/FAIL/ && (/\bd=(\w+)/||/\bg=(\w+)/) && $c{$1}++;END{print"$_ $c{$_}\n" for keys %c}'  file
0
 
uaMozillaAuthor Commented:
Ozo,
  Is there someway to put a zgrep or something in front of all this so that it works with gzipped files?
Thanks
0
 
ozoCommented:
zgrep FAIL file | perl -ne '(/\bd=(\w+)/||/\bg=(\w+)/) && $c{$1}++;END{print"$_ $c{$_}\n" for keys %c}'  
0
 
uaMozillaAuthor Commented:
Perfect. Thanks Ozo.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now