awk Script

Hi,

I have a file (see attachment) which has intervals in the 2nd and the 3rd column.
However, I would like to have an output that adds up the number mentioned in the 4th column to the number in the 2nd column while neglecting the third column.
For example:

chr6    27262994    27263391    194

The final output should add up 194 to 27262994 and should return :

chr6   27262994     27263188

Can you help.

regards,
Amit.

Try

$ echo chr6 27262994 27263391 194 | awk '$3=$2+$4' OFS=\\t
chr6    27262994    27263188    194

OR

if you want to mask column 4 then

$ echo chr6 27262994 27263391 194 | awk '{$3=$2+$4;$4=""}1' OFS=\\t
chr6    27262994    27263188    

For file

$ awk '$3=$2+$4' OFS=\\t file

OR

if you want to mask column 4 then

$ awk '{$3=$2+$4;$4=""}1' OFS=\\t file   
1 Like

Thank you so much Akshay

If some input lines might have more than 4 fields, a simpler approach might be:

awk '{print $1 "\t" $2 "\t" $2 + $4}' K562Tbp.txt