0

I am looking for a command / script that can search for the given string in multiple files under a given tar.gz file and give the wc -l of the string along with specific file name.

I tried the below but doesn't give me any output.

tar -zxf /home/myname/April/maillogs.tar.gz
for f in /home/myname/April/*; do
  grep -l "sent" $f |wc -l
done
4

As already mentioned in comments you can do it in a single zgrep command:

zgrep -c "sent" /home/myname/April/*.tar.gz

From grep manual:

-c, --count

Suppress normal output; instead print a count of matching lines for each input file. With the -v, --invert-match option (see below), count non-matching lines. (-c is specified by POSIX.)

  • Thanks for the quick help. I checked this but my situation is like below. Lets say I have 5 files (file1, file2 .. ) I am looking for number of "sent" pattern in all those 5 files under tar.gz file and list the counts along with file names. Please help.. – Ram May 3 '17 at 11:16
  • @Ram What's the output you get with the command from my answer? – Zumo de Vidrio May 3 '17 at 12:28
  • I get the total number of occurrence for the given string cumulatively from all the 5 files inside the tar.gz. – Ram May 3 '17 at 19:11
  • Hi All, I am looking for total number of "string" occurrence from each file individually. This will help me save a lot of time in my report preparation. Please help me to find a way to achieve this. Thanks in anticipation. – Ram May 4 '17 at 7:49
  • @Ram Update your question with the output you get with the command from my answer. – Zumo de Vidrio May 4 '17 at 9:03

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.