awk condition always TRUE in a loop

Good morning,

I’m sorry this question will seem trivial to some. It has been driving me mad for hours. My problem is the following:
I have these two files:

head <input file>
SNP CHR BP A1 A2 OR P
chr1:751343 1 751343 A T 0.85 0.01
chr1:751756 1 751756 T C 1.17 0.01
rs3094315 1 752566 A G 1.14 0.0093
rs3131972 1 752721 A G 0.88 0.009
rs3131971 1 752894 T C 0.87 0.01
chr1:753405 1 753405 A C 1.17 0.01
chr1:753425 1 753425 T C 0.87 0.0097
rs2073814 1 753474 G C 1.14 0.009
rs2073813 1 753541 A G 0.85 0.0095

and

head <interval file>
1 112667912 114334946
1 116220516 117220516
1 160997252 161997252
1 198231312 199231314
2 60408994 61408994
2 64868452 65868452
2 99649474 100719272
2 190599907 191599907
2 203245673 204245673
2 203374196 204374196

I would like to use a bash script to remove all lines from the input file in which the BP column lies within the interval specified in the input file and in which there is matching of the CHR column with the first column of the interval file.

Here is the code I’ve been working with (although a simpler solution would be welcomed):

while read interval; do
    chr=$(echo $interval | awk '{print $1}')
    START=$(echo $interval | awk '{print $2}')
    STOP=$(echo $interval | awk '{print $3}')

    awk '$2!=$chr {print} $2==$chr && ($3 < $START || $3 > $STOP) {print}' < input_file > tmp
    mv tmp <input file>
done <

My problem is that no lines are removed from the input file. Even if the command

awk '$2==1 && ($3>112667912 && $3<114334946) {print}' < input_file | wc -l

returns >4000 lines, so the lines clearly are in the input file.

Thank you very much for your help.


Source: bash

Leave a Reply