Tuesday, August 21, 2012

FS does not work if putting in awk body

Just noticed this by double-checking the weird result I got:

For example , you have a text file with some empty columns in lines, like:

JUNCTION.100681___1___6___1___6___JUNCTION.100681___ ___17___ ___12

(which is tab-separated, and the 7th and 9th columns are empty)

awk '{FS="\t";OFS="\t"; print $1,$2+$7,$3+$8,$4+$9,$5+$10}' will output:


It takes $7 as 17 and $8 as 12, which is wrong. The right output should be:


It seems FS="\t" does not work properly in the body of AWK.

The right solution is:

awk 'BEGIN{FS="\t"}{OFS="\t";print $1,$2+$7,$3+$8,$4+$9,$5+$10}'


awk -F"\t" '{OFS="\t";print $1,$2+$7,$3+$8,$4+$9,$5+$10}'


awk -v FS="\t" '{OFS="\t";print $1,$2+$7,$3+$8,$4+$9,$5+$10}'

I am not sure if this is a bug.
btw, I am using GNU Awk 3.1.5.

No comments:

Post a Comment