Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grep output including filename but from cat input?

Status
Not open for further replies.

scottyjohn

Technical User
Nov 5, 2001
523
GB
Hi all,
I am currently using the following command to cat a bunch of .gz files, gunzip -c them, and then grep for a text string which sends to output file

Code:
cat *.gz |gunzip -c |grep "person_dbid = 10353" >Skillchange8.txt

What I need is to have the filename of the original file where the text was found, as part of the output. Is this possible? Im a Solaris newbie!

Cheers

John
[smile]
 
Maybe you can avoid the cat part;

grep "person_dbid = 10353" >Skillchange8.txt

This will also print the filename;

ie.
grep "alan" list*

list1:alan
list3:alan



 
Sorry, that won't work. I am sure this was just an oversight. You were correct about the "cat" being the reason for this not working.

You need to include a wildcarded filename argument (example below *lis* or lis* or just *) on the command line in order for it to return the filename: in the returned data.

grep "person_dbid = 10353" *lis* >Skillchange8.txt


A great teacher, does not provide answers, but methods to teach others "How and where to find the answers"

bsh

35 years Bell, AT&T, Lucent, Avaya
Tier 3 for 25 years and counting
 
Yes, it was a typo.
Luckily the second part of my response showed that quite clearly, what with it having a filename and a wildcard.

However, this is still not going to solve the OP problem, because unless the files are unzipped I can't see how you can pass 'grep' the filename.
I'm sure someone will come up with something though.
 
This is close, but does not provide the pre tar and zipped filenames.

drsun [603895]-> gzgrep -l rd.*dp_mgr *.gz
docs.tar.gz
docs2.tar.gz
drsun [603896]-> gzgrep rd.*dp_mgr *.gz
docs.tar.gz:mad: rd dp_mgr 0x3fb9a2+0x2*0x0 "Cnt_ext"nxn
docs.tar.gz:rd dp_mgr 0xbf0000+0xc*0x0 "Ext_e"n270(3lxn)
docs.tar.gz:rd dp_mgr 0xef0000+0xc*0x0 "Ad_stn"n270(3lxn)
docs.tar.gz:rd dp_mgr 0x770000+0x1e*0x0 "Ustn_e1"n271(3lxn16ctxn)
docs2.tar.gz:mad: rd dp_mgr 0x3fb9a2+0x2*0x0 "Cnt_ext"nxn
docs2.tar.gz:rd dp_mgr 0xbf0000+0xc*0x0 "Ext_e"n270(3lxn)
docs2.tar.gz:rd dp_mgr 0xef0000+0xc*0x0 "Ad_stn"n270(3lxn)
docs2.tar.gz:rd dp_mgr 0x770000+0x1e*0x0 "Ustn_e1"n271(3lxn16ctxn)

drsun [603898]-> pwd
/usr/bin
drsun [603899]-> ls *grep*
bzegrep* bzgrep* fgrep* gzegrep* gzgrep* pgrep*
bzfgrep* egrep* grep* gzfgrep* nisgrep* zipgrep*



A great teacher, does not provide answers, but methods to teach others "How and where to find the answers"

bsh

35 years Bell, AT&T, Lucent, Avaya
Tier 3 for 25 years and counting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top