我有两个文件获得以下信息。我需要比较FILE2中匹配的文件和打印行。
FILE1.TXT
martin01
kevin01
phoenix01
samson01
edward01
FILE2.TXT
martin01.test.com empid: 9874712
martin01.test1.com empid:0972292
kevin01.test.com empid: 3297203
kevin01.test.com empid: 3297203
phoenix01.test.com empid: 9872219
phoenix01.test1.com empid:9803994
samson01.test.com empid: 0983903
samson01 empid: 7665758
edward01.test.com empid: 0979072
edward01.test2.com empid: 748840
gregory01.test.com empid: 657758
clevin01.test.com empid:6589598
预期输出应该是这样的:
martin01.test.com empid: 9874712
martin01.test1.com empid:0972292
kevin01.test.com empid: 3297203
kevin01.test.com empid: 3297203
phoenix01.test.com empid: 9872219
phoenix01.test1.com empid:9803994
samson01.test.com empid: 0983903
samson01 empid: 7665758
edward01.test.com empid: 0979072
edward01 empid: 748840
这是我写的,但是它错了..
awk 'FNR==NR {a[$1]; next} $1 in a' FILE1.txt FILE2.txt
答案 0 :(得分:2)
您需要将FS设置为.
:
awk -F'[.[:space:]]' 'FNR==NR {a[$1]; next} $1 in a' FILE1.txt FILE2.txt
答案 1 :(得分:2)
替代awk
$ grep -Ff file1 file2
martin01.test.com empid: 9874712
martin01.test1.com empid:0972292
kevin01.test.com empid: 3297203
kevin01.test.com empid: 3297203
phoenix01.test.com empid: 9872219
phoenix01.test1.com empid:9803994
samson01.test.com empid: 0983903
samson01 empid: 7665758
edward01.test.com empid: 0979072
edward01.test2.com empid: 748840
请注意,匹配位于行中的任何位置,而不仅限于第一个字段。如果您的字段是不相交的集合,请使用此选项。