I am attempting to grep a list of files for a string an and then only extract the 3rd and 4th field of from the line. That's easy. But I want to prefix the line with the filename that the information came from.
for filename in `ls -1 *.txt'
do
grep search_text $filename | awk '{print $3" "$4}'
done
This displays the 3rd and 4th field of all lines in the list of files as in:
Field3 Field4
Field3 Field4
but I would like to see
a.txt Field3 Field4
b.txt Field3 Field4
Thanks...this got me on the right track but I left something out that causes this solution to have a problem. The files are am searching through are gzipped. When I try to gunzip the file and pipe the data to awk, awk now things the FILENAME is standard in. Any thoughts?
for filename in `ls -1 *.txt.gz`
do
{ gunzip < $filename 2>>/dev/null; echo $?>>rc.txt; } | awk '/search_text/ {print $3,$4}'
done