我有2个文件
file1.txt [syntax field1:filed2:field3:...]
123456:07102015174037:100 --> this should be matched
123457:03102015174037:354
123456:03102015174037:1
1234556:03102015174037:0
file2.txt [syntax field3:filed4:field1:...]
100:03102015174037:123456 --> this should be matched
101:03145415174037:1234556
我想检查一下 来自file1.txt的field1&组合field2存在于field3&的file2.txt中。 FIELD1
在结束时,我想只打印匹配
的file1.txt内容所以我最终做了[只获得匹配的列]
awk -F ':' '{print $1,$2}' file1.txt >> tmpfile1.txt
awk -F ':' '{print $1,$3}' file2.txt >> tmpfile2.txt
grep -f tmpfile1.txt tmpfile2.txt> match.txt
grep -f match.txt file1.txt >> file1updated.txt
cat fil1updated
100:03102015174037:123456
是否有一步&这样做的有效方式[就像加入SQL中的列一样]
答案 0 :(得分:2)
您当然可以内联运行。但我更喜欢创建一个awk脚本(join.awk
)
NR==FNR {
a[$1 FS $3] = $0; next
}
$3 FS $1 in a {print a[$3 FS $1]}
然后你可以通过运行
来获得结果awk -F: -f join.awk file2.txt file1.txt