Reputation: 64004
I have a following input data:
AATGCCACGTTGAGAGGCGTTCGCGGAAGGCGCG 44 40.000
AATGCCACTCGGGTCCGAGATGGCGGATCTCCAC 35 37.000
AATGCCAGAGCTGTGGTAGTGTGAAAGCAGCAGT 13 13.000
What I want to do is to substract column 3 by column 2 yielding:
AATGCCACGTTGAGAGGCGTTCGCGGAAGGCGCG -4
AATGCCACTCGGGTCCGAGATGGCGGATCTCCAC 2
AATGCCAGAGCTGTGGTAGTGTGAAAGCAGCAGT 0
But why this awk doesnt' work:
$ awk '{dif =($3 - $2)}END{print dif}' input.txt
Upvotes: 0
Views: 7227
Reputation: 131600
Because you only print out the difference once, at the end, which means everything but the last row is effectively discarded. (The END
block runs only once, when the entire input has been read and processed) You want this instead:
awk 'NF > 0 { print $1 "\t" ($3 - $2) }' input.txt
The NF > 0
prevents awk from printing meaningless 0s for empty lines.
Upvotes: 4