1

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  -
AishwaryaKulkarni
  • 774
  • 1
  • 8
  • 19

1 Answers1

2

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.

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • hi karakfa thanks a lot I should have been more specific , it was column3-(column3-column2/2) such that instead of 49944618 it will be 99888401-99887566=835 and divide that with 2 which will give 417.5 and I subtract 417 from 99888401 to get 99887984. I just don't know how to add the division in this ?: clause. – AishwaryaKulkarni Oct 18 '16 at 01:23
  • `$3-($3-$2/2) = $2/2` did you mean `$3-($3-$2)/2`? If so replace `$2/2` in the script with `($3-$2)/2` – karakfa Oct 18 '16 at 01:36
  • Oh yeah right, sorry I confused myself , when it was so obvious, Thanks! – AishwaryaKulkarni Oct 18 '16 at 01:43