tags:

views:

267

answers:

1

Hi,

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
+3  A: 

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.

David Zaslavsky