dam4l10 dam4l10 - 6 months ago 15
Bash Question

Extract rows where value in one column equals value in another column +1?

I have a tab delimited text file that looks like this:

1 10019 10020 rs775809821
1 10055 10055 rs768019142
1 10107 10108 rs62651026
1 10108 10109 rs376007522
1 10128 10128 rs796688738
1 10138 10139 rs368469931
1 10144 10145 rs144773400
1 10146 10147 rs779258992
1 10149 10150 rs371194064
1 10165 10165 s796884232


I want to extract the rows in which the value in column 2 is equal to the value in column 1 + 1 and direct them to a new file. So for the above example, the desired output would be:

1 10019 10020 rs775809821
1 10107 10108 rs62651026
1 10108 10109 rs376007522
1 10138 10139 rs368469931
1 10144 10145 rs144773400
1 10146 10147 rs779258992
1 10149 10150 s371194064


I think this can be accomplished using awk, but I'm not sure where to start. Any input would be greatly appreciated.

Answer
awk '$3 == $2 + 1' < input > output
Comments