问题
I need to intersect the a file from a list of locations as like this
File 1
cat02 2 5
cat02 2 3
cat03 2 3
File2
cat02 1 xxx xxx
cat02 2 xxx sss www
cat02 3 swe ede rrr
cat02 4 aqw ede efd
cat02 5 aws ede as
cat02 6 aqw
cat03 1 aaa
cat03 2 wer
cat03 3 ddddd
expected output
cat02 2 xxx sss www
cat02 3 swe ede rrr
cat02 4 aqw ede efd
cat02 5 aws ede as
cat02 2 xxx sss www
cat02 3 swe ede rrr
cat03 2 wer
cat03 3 ddddd
For this purpose I used this code
awk 'FNR==NR{
start[$1]=$2
till[$1]=$3
next}$2>=start[$1] && $2<=till[$1]' File1 File2
But if I am trying to grep two ranges for cat02 I only get the output for the last range read instead of both ranges:
$ awk 'FNR==NR{
start[$1]=$2
till[$1]=$3
next}$2>=start[$1] && $2<=till[$1]' file1 file2
cat02 2 xxx sss www
cat02 3 swe ede rrr
cat03 2 wer
cat03 3 ddddd
回答1:
$ cat tst.awk
NR==FNR {
    vals[$1,$2] = $0
    next
}
{
    for (i=$2; i<=$3; i++) {
        key = ($1 SUBSEP i)
        if (key in vals) {
            print vals[key]
        }
    }
}
$ awk -f tst.awk File2 File1
cat02 2 xxx sss www
cat02 3 swe ede rrr
cat02 4 aqw ede efd
cat02 5 aws ede as
cat02 2 xxx sss www
cat02 3 swe ede rrr
cat03 2 wer
cat03 3 ddddd
回答2:
$ awk 'NR==FNR {s[$1]=$2;e[$1]=$3;next} 
                $2>=s[$1] && $2<=e[$1]' file1 file2
cat02 2 xxx sss www
cat02 3 swe ede rrr
cat02 4 aqw ede efd
cat02 5 aws ede as
cat03 2 wer
cat03 3 ddddd
actually it's the identical code with yours, perhaps your line endings is a problem.  You can see with cat -A file.  
来源:https://stackoverflow.com/questions/62349346/intersecting-text-files-by-its-location-from-a-list