LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 09-28-2011, 03:44 PM   #1
beeth2006
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?
Thanks!
 
Old 09-28-2011, 06:00 PM   #2
snooly
Member
 
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
beeth2006
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
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can try by means of a little script, like this:
Code:
#!/bin/bash
while read file
do
  result=$(tar xf access.tar.gz -O "$file" | grep fail)
  if [[ -n $result ]]
  then
    echo "$result" | awk -v file="$file" '{print file ":", $0}'
  fi
  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.
 
  


Reply


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


All times are GMT -5. The time now is 05:18 AM.

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