I Phoenix
I Phoenix

Reputation: 59

read different fields and pass on to awk to extract those fields

Probably this is answered somewhere, but the things I have explored is not matching my need.

I would like to read different fields from one file (FILE1) and pass this on to a awk script, which can extract those fields from another file (FILE2).

FILE1

1   156202173   156702173
2   26915624    27415624
4   111714419   112214419

so read lines from this file and pass it on to the following script

awk ' BEGIN {FS=OFS="\t"};
{if ($1==$1 && $2>= $2 && $2<= $3 ) {print $0}}' FILE2 > extracted.file

The FILE2 looks like this;

1   156202182   rs7929618
16  8600861 rs7190157
4   111714800   rs12364336
12  3840048 rs4766166
7   20776538    rs35621824

so the awk script print only when there is a match with the first field and the value falls between the 2nd and 3rd field. Expected output is

1   156202182   rs7929618
4   111714800   rs12364336

Thanks so much in advance for your response.

Upvotes: 0

Views: 52

Answers (1)

karakfa
karakfa

Reputation: 67467

there should be plenty of similar questions but writing the script is faster than looking up.

$ awk 'NR==FNR{lower[$1]=$2; upper[$1]=$3; next} 
       lower[$1]<$2 && $2<upper[$1]' file1 file2

1   156202182   rs7929618
4   111714800   rs12364336

Upvotes: 1

Related Questions