FILENAME of awk with stdin

Hello: How can I print out the FILENAME with awk when the input is from STDIN?

   zcat SRR1554449.fq.gz  | awk ' (length($2) >= 300) {print FILENAME}'

this will print out

-
-
-
....

as when awk reads from the standard input, and FILENAME is set to "-". But I am expecting sth like:

SRR1554449.fq.gz
SRR1554449.fq.gz
SRR1554449.fq.gz
SRR1554449.fq.gz
......

Is there a possible solution for this problem in awk?
Thanks!

As

echo SRR1554449.fq.gz  | awk '{print FILENAME}'
-

does print the "file name", I suspect you want SRR1554449.fq.gz to be printed? Without additional measures: you can't, as it is gone when zcat opens it and produces output to be piped into awk .

1 Like

Another approach:-

zcat -v SRR1554449.fq.gz 2> out.txt | awk '
                        NR == 1 {
                                getline F < "out.txt"
                        }
                        ( length($2) >= 300 ) {
                                print substr(F,1,index(F,":")-1)
                        }
        '
1 Like

Perhaps you can pass the filename separatedly? For example

#!/bin/sh
file=SRR1554449.fq.gz
zcat "$file" | awk -v file="$file" ' (length($2) >= 300) {print file}' 
1 Like

Thanks!
Answer by MadeInGermany is what I want.

I was about to propose sth. like

file=SRR1554449.fq.gz
{ echo "$file"; zcat "$file"; } | awk 'NR==1'
SRR1554449.fq.gz
1 Like