Question

Would like to extract all the lines from first file, if the first file first field is falls within a range of Second file third field (Start Range) and fourth field (End Range)

Have tried the below command and incomplete , please help

awk -F"," 'NR==FNR{ start[$1] = $3; end[$1] = $4; next } ( ($1 >= start[$1]) && ($1 <= end[$1]) )' f22.txt f11.txt

f11.txt:

12,abc,ppp
15,def,ppp
20,xyz,ppp
6,hij,ppp
8,mno,ppp

f22.txt

Data1,Data2,Start,End
qqq,www,1,5
qqq,www,10,15
qqq,www,7,9

Desired Output:

12,abc,ppp
15,def,ppp
8,mno,ppp
Was it helpful?

Solution

this line should do the job:

awk -F, 'NR==FNR{s[NR]=$3;e[NR]=$4;n++;next}
     {for(i=1;i<=n;i++)if($1>=s[i]&&$1<=e[i]){print;break}}' file2 file1

OTHER TIPS

awk with grep:

awk -F, '{for (i = $3; i <= $4; i++) {print "^"i","}}' input2 | grep -f - input1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top