awk script modification - treat certain files differently

awk  'BEGIN{OFS=","} FNR == 1
			{if (NR > 1) {print fn,fnr,nl}
                        fn=FILENAME; fnr = 1; nl = 0}
                        {fnr = FNR}
         		/UNUSUAL/ && /\.gz/ ~ /FILENAME/ {nl++}
                        <'{system ("gunzip -cd FILENAME")}'
         	END                    {print fn,fnr,nl}
        ' /tmp/appscraps/* > /tmp/test.txt

the above scans all files in a given directory. prints the file name, number of lines in each file and number of lines found containing 'ERROR'.

im now trying to make it so that the script executes a command if any of the file it reads in isn't a regular file. i.e., if the file is a gzip file, then run a particular command.

the bolded is my attempt to include the gunzip command in there. unfortunately, i cannot "gunzip" all the files in the directory beforehand. this is because not all files in the directory will be "gzip" type. some will be regular files.

so i need the script to treat any .gz file it finds a different way so it can read it, count and print the number of lines that's in it, and the number of lines it found matching the pattern supplied (just as it would if the file had been a regular file).

change:

 /UNUSUAL/ && /\.gz/ ~ /FILENAME/ {nl++}
 <'{system ("gunzip -cd FILENAME")}'

to:

/UNUSUAL/ && FILENAME ~ /\.gz$/ {nl++}
{
cmd="gunzip -cd " FILENAME
cmd; close(cmd)
}
1 Like

i tried that, but it didn't seem to work. it printed the same file over and over, thousands of times:

awk  'BEGIN{OFS=","} FNR == 1
			{if (NR > 1) {print fn,fnr,nl}
                        fn=FILENAME; fnr = 1; nl = 0}
                        {fnr = FNR}
                        /UNUSUAL/ && FILENAME ~ /\.gz$/ {nl++}
                        {
                            cmd="gunzip -cd " FILENAME
                            cmd; close(cmd)
                         }
         	END                    {print fn,fnr,nl}
        ' /tmp/appscraps/* > /tmp/test.txt