gc5
gc5

Reputation: 9868

How to append lines to a new file with AWK

I am trying to append lines to some new files with awk in this way:

#!/usr/bin/awk -f

BEGIN { 
    FS = "[ \t|]"; }
{ 
    print $5 "\t" $13 "\t" $14 >> "./bed/" $5 ".bed";
}
END { 
}

New file is created with filename derived from a field of awk input file (5th field). I am unable to execute this script since it fails with

awk: ./blast2bed.awk:6: (FILENAME=blastout000 FNR=1) fatal: can't redirect to `./bed/AY517392.1.bed' (No such file or directory)

Any hints?

Thanks

Upvotes: 1

Views: 6720

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85795

The directory bed has to exist so create it first with mkdir bed either before you run your script or in the BEGIN block. You should also add brackets around the output file:

print $5"\t"$13"\t"$14 >> ("./bed/"$5".bed")

Notes: You don't need to end lines with ; if you have a single statement per line and the BEGIN and END blocks are optional.

Upvotes: 2

Related Questions