Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-28-2011, 03:44 PM   #1
LQ Newbie
Registered: Aug 2007
Posts: 7

Rep: Reputation: 0
How to grep string in tar.gz file with matched filenames?

I got a log file called access.tar.gz, which has 100 text files named: a1, a2, a3, , a100. I want to search for string "fail" in the access.tar.gz file, WITHOUT creating a temp folder by uncompress the .tar.gz file, and need to know which aX file(s) has "fail" string. How can I get the output like:

a2 <line with "fail" string>
a2 <another line with "fail" string>
a6 <line with "fail" string>
a11 <line with "fail" string>

( that is, the matched filenames, with matched lines ).

I can use "zgrep -a fail access.tar.gz" to get all the matched lines, WITHOUT the matched filenames, and I don't see any option in zgrep to show filenames. And I think it's very common users want to know which file has the matched lines.

How can I do it with single line of command(s) to do this without writing a script?
Old 09-28-2011, 06:00 PM   #2
Registered: Sep 2011
Posts: 124

Rep: Reputation: Disabled
I made a few small test files, and then tarred them up with a gzip as well. When I use zgrep -a, it does seem to tell me the file name. But maybe that's because my files are very short? Approximately how long are your files?

Here's the type of output I get:

zgrep -a elephant testtar.tar.gz
a3.txt0000664000175000017500000000001111640713531010247 0ustar snoolysnoolyelephant

versions of software used:
zgrep -V
z$grep (gzip) 1.3.12

tar --version
tar (GNU tar) 1.23
Old 09-29-2011, 10:08 AM   #3
LQ Newbie
Registered: Aug 2007
Posts: 7

Original Poster
Rep: Reputation: 0
Yes, i think your files are short so that the matched file(s) are only shown once, not for every matched lines. But thanks!
Old 09-29-2011, 11:00 AM   #4
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
You can try by means of a little script, like this:
while read file
  result=$(tar xf access.tar.gz -O "$file" | grep fail)
  if [[ -n $result ]]
    echo "$result" | awk -v file="$file" '{print file ":", $0}'
  unset result
done < <(tar tf access.tar.gz | grep -v '/$')
However it may require a lot of time if the size of the archive is huge, because it is actually unzipped and extracted, even if the output is sent to the standard output to avoid the creation of temporary files.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
possible with grep? find string and then output that and the rest of the file captain_cthulhu Linux - Newbie 4 05-18-2009 01:09 PM
grep for string in file after a certain part of the file B-Boy Programming 6 02-18-2009 08:49 AM
How to grep for a string in a file kaprasanna Linux - Newbie 3 01-06-2009 07:29 AM
Grep string and redirect the rest to a file twantrd Programming 6 06-02-2006 08:37 PM
Grep String Search, and identify source file. carl0ski Linux - General 4 01-21-2006 09:15 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:20 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration