kriegsmaschine
kriegsmaschine

Reputation: 108

Pass bash loop variable through pipe to AWK

I have the following bash script:

for file in *.gz; do 
    zgrep @ $file | awk '{split($0,a,":");  
    print $file"\t@RID\tT1:"a[1]"\tT2:"a[2]"\tT3:"a[3]}' > out.csv; 
done

I want to create an output csv/tbi file that has the file name placed into the first column of the zgrep output piped through awk.

The error is:

"awk: illegal field $(), name "file" input record number 1, file
source line number 2"

I am expecting to get this:

Column headers: (these don't need to be in the actual output file I just put it here for clarity's sake since I can't find how to separate with tabs on stackoverflow):

filename | RID | T1 | T2 | T3

output row:

FILENAME.gz @RID T1:text1 T2:text2 T3:text3

... more rows here...

If I remove the $file in the print command it will work (needless to say it omits the filename). I have tried the following option inside the awk call and outside but to no avail:

-v i=$file 

Any suggestions or workarounds would be appreciated.

Upvotes: 0

Views: 1252

Answers (1)

Ed Morton
Ed Morton

Reputation: 203684

for file in *.gz; do 
    zgrep @ "$file" |
    awk -F':' -v OFS='\t' -v file="$file" '{
        print file, "@RID", "T1:" $1, "T2:" $2, "T3:" $3
    }' 
done > out.csv

Upvotes: 1

Related Questions