AishwaryaKulkarni
AishwaryaKulkarni

Reputation: 784

math in awk columns

I am trying to execute the following awk command

 awk '{ if($3-$2 >= 1000) print $1"\t"$3-1000"\t"$3"\t"$4"\t"$5 ; else if($3-$2 < 1000) print $1"\t"$3-($3-$2/2)"\t"$3"\t"$4"\t"$5 }'file

where if the subtraction between column 3 and 2 is > 1000 then follow a certain condition , else follow another condition in which column 2 is column3-(column3-column2/2) in whole number. The file looks as follows:

  chrX  99885864    99887481    I7  -
  chrX  99887566    99888401    I6  -
  chrX  99888537    99888927    I5  -
  chrX  99889027    99890174    I4  -
  chrX  99890250    99890554    I3  -
  chrX  99890744    99891187    I2  -
  chrX  99892102    99894941    I1  -
 chr20  49552800    49557401    I8  -
 chr20  49557493    49557641    I7  -
 chr20  49557747    49558567    I6  -

Upvotes: 1

Views: 466

Answers (1)

karakfa
karakfa

Reputation: 67467

you need to overwrite awk defaults or format explicitly.

$ awk -v OFS='\t' '{$2=sprintf("%d",$3-(($3-$2>=1000)?1000:$2/2))}1' file

chrX    99886481        99887481        I7      -
chrX    49944618        99888401        I6      -
chrX    49944658        99888927        I5      -
chrX    99889174        99890174        I4      -
chrX    49945429        99890554        I3      -
chrX    49945815        99891187        I2      -
chrX    99893941        99894941        I1      -
chr20   49556401        49557401        I8      -
chr20   24778894        49557641        I7      -
chr20   24779693        49558567        I6      -

ps. I think your formula is not correct for the else condition, otherwise make the change in my script.

Upvotes: 2

Related Questions