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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I would like to use a stream of filenames from a pipe 1

Status
Not open for further replies.

menski

Technical User
Oct 13, 2002
18
GB

I have a script which has got so far:

file .* | awk -F: '/text/ {print $1}'

as you can see, it hasn't got very far - but it does output a list of the filenames of files identified as text files (rather than directories, binaries or etc). The next thing I need Awk to do is open each of these files and look for a text string (eg 'mother').

I would guess that I would need to put the value of $1 into a variable (chk = $1)and then I need to search the file. Ideally I need to be able to nest something like:

{/mother/ {print chk} chk}
close chk}

but I can't work out how to do it. Any advice would be gratefully received!

duncan

 
You need to use getline to read the files, as in this (untested) example.

file .* | awk -F: '
/text/ {
fn = $1
print fn
while ((getline < fn) > 0) {
if ($0 ~ /mother/) print
}
}' CaKiwi
 
To CaKiwi: Smashing, thanks! I'll try it tomorrow (I'm at work now) and get back with the result.

dmk
 
Yes, that's great. Actually, I feel a bit of an idiot, it is very similar to an example in the Gawk Info file with which I had already been (unsuccessfully) experimenting.

Thanks for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top