Rashedul Islam
Rashedul Islam

Reputation: 939

Subtracting two numeric columns

I need to subtract column two from three. So, I ran the following code on Linux server awk '$3 - $2' in.bed >out.bed, the output file generated was identical to the input file. I can't understand why this command is not working! Can anyone help?

The in.bed was:

chr19   2255482 2259550
chr19   2270962 2272924
chr19   2326618 2327833
chr19   2329714 2333233

Upvotes: 1

Views: 261

Answers (1)

Wintermute
Wintermute

Reputation: 44023

The way you put it, $3 - $2 is parsed as a condition: Those lines where $3 - $2 is not zero are selected and printed (because printing the line unchanged is the default action).

You can do what you want like this:

awk '{ print $3 - $2 }' in.bed > out.bed

The {} signify that what is enclosed in them is an action, and because there is no attached condition, { print $3 - $2 } is executed unconditionally for all lines.

Upvotes: 4

Related Questions