2

I'm trying to figure out the number of lines that contain the strings "event" and "type". The files I want to search through are in multiple folders and are zipped. I'm able to get an aggregated count of what I'm looking for, but my goal is to have the count displayed for each file. This is what I'm currently using:

zcat /folder1/{folderA,folderB,folderC}/folder2/folder3/result-2018-05-1* | grep 'event' | grep 'type' | wc -l

And my output is:

86446

But I want my output to look like:

result-2018-05-10.log.gz: 1000
result-2018-05-11.log.gz: 3000
result-2018-05-12.log.gz: 20000
result-2018-05-13.log.gz: 4446
result-2018-05-14.log.gz: 12000
result-2018-05-15.log.gz: 10000
result-2018-05-16.log.gz: 15000
result-2018-05-17.log.gz: 5000
result-2018-05-18.log.gz: 6000
result-2018-05-19.log.gz: 10000

Any suggestions?

1

For only two tests, this should be enough:

zgrep -E -c 'event.*type|type.*event' /folder1/{folderA,folderB,folderC}/folder2/folder3/result-2018-05-1*

Testing if a line contains type and event is the same as testing if it contains type followed later by event or event followed later by type. This wouldn't scale well if a 3rd test was needed.

Then adding something like | sed 's#^.*/##' should give the exact result in the question.


UPDATE:

For something more generic, a loop seems better, so:

for i in /folder1/{folderA,folderB,folderC}/folder2/folder3/result-2018-05-1*; do
    printf '%s: ' "$(basename "$i")"
    zcat < "$i" | grep 'filter1' | grep 'filter 2' | grep 'filter3' | wc -l
done
  • I'm actually grepping a lot of strings, I limited it to two to simplify the question. But this is still good to know! Thanks! – Sam May 18 '18 at 21:04
  • so I added a more generic case. would that be fine? – A.B May 18 '18 at 21:15
  • if you think this was the solution you should mark it as the solution – A.B May 19 '18 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.