通过列表中的位置相交文本文件 [英] Intersecting text files by its location from a list
本文介绍了通过列表中的位置相交文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要像这样从位置列表中将文件相交
I need to intersect the a file from a list of locations as like this
文件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
预期产量
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
但是,如果我尝试对cat02的两个范围进行grep转换,那么我只会获得读取的最后一个范围的输出,而不是两个范围:
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
推荐答案
$ 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
这篇关于通过列表中的位置相交文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文