有两列作为输入文件的grep [英] Grep file with two columns as input
本文介绍了有两列作为输入文件的grep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文件,其中包含线,如:
I have a file containing lines like:
"ALMEREWEG ";" 45 ";" ";"ZEEWOLDE ";"3891ZN"
"ALMEREWEG ";" 50 ";" ";"ZEEWOLDE ";"3891ZP"
"ALMEREWEG ";" 51 ";" ";"ZEEWOLDE ";"3891ZN"
"ALMEREWEG ";" 52 ";" ";"ZEEWOLDE ";"3891ZP"
"ALMEREWEG ";" 53 ";" ";"ZEEWOLDE ";"3891ZN"
和我有第二个文件,其中包含线,如:
and I have a second file containing lines like:
3891ZP;50;
3891ZN;53;A
3891ZN;53;B
3891ZN;54;
现在我想根据第二个文件,其中的格局到grep的第一个文件:
Now I want to grep the first file based on the pattern of the second file, where:
A)2号文件的第1列是第一个文件的第5列present;和
A) the 1st column of the 2nd file is present in the 5th column of the 1st file; and
B)中的第2个文件的第2列是在第1文件的第2列present
B) the 2nd column of the 2nd file is present in the 2nd column of the 1st file.
我的问题:如何做到这一点
更新2013年7月7日:我更新文件2格式,以反映第三列(数量足够)
Update 7 July 2013: I updated file2 format to reflect the third column (number suffices).
推荐答案
有一个办法 AWK
:
awk -F';' '
NR==FNR {
a[$1]=$2
next
}
{
line=$0
gsub(/\"/,"")
gsub(/ *; */,";")
if (a[$5]==$2) {
print line
line=""
}
}' file2 file1
输出
"ALMEREWEG ";" 50 ";" ";"ZEEWOLDE ";"3891ZP"
"ALMEREWEG ";" 53 ";" ";"ZEEWOLDE ";"3891ZN"
这篇关于有两列作为输入文件的grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文