0

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.

1 Answer 1

1
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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.