1
votes

I have some text files. I need to do the subtraction between second and fourth columns in each file. The subtracted values should print to the original files as fifth column. How can I do this with awk or sed?

HII 62.0    HII 35.1
MEE 21.3    MEE 21.3
GLL 42.3    GLL 18.5
ASS 105.9   ASS 105.9
RRG 65.6
GLL 48.3
SES 83.5    

Desired output

HII 62.0    HII 35.1   26.9
MEE 21.3    MEE 21.3    0
GLL 42.3    GLL 18.5   23.8
ASS 105.9   ASS 105.9   0
RRG 65.6
GLL 48.3
SES 83.5

If the third and fourth columns are blank, no need to subtract.

2

2 Answers

1
votes

This might work for you (GNU sed & Bash):

sed -ri '/^\S+\s+(\S+)\s+\S+\s+(\S+)/s//echo "&\t$(echo \1-\2|bc)"/e' file
5
votes
awk 'NF == 2 { print }
     NF == 4 { print $0, $2 - $4 }'

That could all be fitted onto one line, but it clearer what it is doing when it is spread over two lines.

If you want more control over the format, you can use printf() instead of just print.

After sanitizing trailing spaces in the data, it produces:

HII 62.0    HII 35.1 26.9
MEE 21.3    MEE 21.3 0
GLL 42.3    GLL 18.5 23.8
ASS 105.9   ASS 105.9 0
RRG 65.6
GLL 48.3
SES 83.5