[英]awk when matching and printing two files based on one column
I have two files (delimiter is tab) 我有两个文件(分隔符是制表符)
File1: db.txt
db.txt
: db.txt
string1 string2 string3 001 string4
string5 string6 string7 002 string8
string9 string10 string11 003 string12
File2: query.txt
query.txt
: query.txt
id1 001
id2 003
and I wand to match file1 and file2 and print (if there is a match) column 1 to 5 of db.txt
and column 1 of query.txt
和我的魔杖匹配file1和file2并打印(如果匹配)
db.txt
1列到第5列以及query.txt
1列
I tried using awk, here my code: 我尝试使用awk,这里是我的代码:
awk 'BEGIN{FS=OFS="\t"}NR==FNR{a[$2]=$4;next}$4 in a{print $1,$2,$3,$4,$5,a[$1]}' query.txt db.txt
but I only get a file with matches (? I at least think so) and columns of the db.txt
file 但我只得到一个匹配文件(?我至少这么认为)和
db.txt
文件的列
EDIT: my more complex db2.txt
编辑:我更复杂的
db2.txt
string1 <TAB> string2 <TAB> 9999 abc dehi [way:pn9999] <TAB> 001 <TAB> org; string3 string4
string5 <TAB> string6 <TAB> 9999 dwd meti [way:pn8999] <TAB> 002 <TAB> org2; string7
string8 <TAB> string9 <TAB> 9999 dwd meti [way:pn7999] <TAB> 003 <TAB> org4; string10
AMD$ cat f1
id1 001
id2 003
AMD$ cat f2
string1 string2 string3 001 string4
string5 string6 string7 002 string8
string9 string10 string11 003 string12
AMD $ awk 'NR==FNR {a[$2]=$1; next} {for(i in a) if(index($0,i)) print a[i], $0}' f1 f2
id1 string1 string2 string3 001 string4
id2 string9 string10 string11 003 string12
You can use awk
like this: 你可以像这样使用
awk
:
awk 'BEGIN{FS=OFS="\t"} FNR == NR { a[$2] = $1; next }
$4 in a { print $0, a[$4] }' query.txt db.txt
string1 string2 string3 001 string4 id1
string9 string10 string11 003 string12 id2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.