Search compressed files with awk and get FILENAME

I have many compressed files I want to search using awk and want to print some file contents along with the filename it came from on each output record (I simplified awk command).

Here are the results with the files uncompressed:

awk '{print FILENAME, $0}' test*.txt
test1.txt from test1 file
test2.txt from test2 file
test3.txt from test3 file

If I compress the files, I use find and zcat to pass to awk but the filename does not print:

find . -name "test*.txt.Z" -exec zcat {} \; | awk '{print FILENAME, $0}' 
- from test1 file
- from test2 file
- from test3 file

How can I use awk to search compressed files and display the filename with each output line?

Try:

find . -name "test*.txt.Z" -print | awk '{F=$0; while(("zcat "F| getline) > 0) print F, $0; close("zcat "F)}'

or

find . -name "test*.txt.Z" -print | while read FILE
do
    zcat "$FILE" | awk -v F="$FILE" '{ print F, $0 }'
done
1 Like

Another approach:

find . -name "test*.txt.Z" -exec zcat -v {} \; 1>&1 2>&1 | awk ' {
        sub(/[ \t]+[0-9]+\.[0-9]%/,x,$0)
        r = match($0, /\..*:/)
        if(r) {
                f = substr($0,RSTART,RLENGTH-1)
                print "Filename: " f
                $0 = substr($0,RSTART+RLENGTH)
                sub(/^[ \t]+/,x,$0)
        }
        print $0
} '
1 Like

Chubler_XL,
Your one line solution, which I was looking for, worked perfectly! Thank you.

bipinajith,
Thank you as well, however, I will need to study your solution to determine what it's doing.