AWK;通过文件1搜索文件2 [英] awk; searching file2 by file1
本文介绍了AWK;通过文件1搜索文件2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我寻觅高有低,但没有找到确切的code awk的。
I have searched high and low, but not finding the exact code for awk.
我有2个文件。
文件1(单柱):
I have 2 files. File 1 (single column):
1407859648
1639172851
1427051689
1023011285
1437152683
1508869405
1790775963
1932373552
文件2(三列):
File 2 (three columns):
1790775963,1932373552,65
1639206006,1437337425,15
1265418669,1477541563,145
1053424648,1316944317,182
1184611535,1821014457,26
1003906082,1134327133,152
1376530121,1841236684,168
1316921570,1962555771,23
1396962627,1184732489,87
1194958421,1255333456,113
1538156732,1336215482,62
文件1和2具有不同数量的记录。
我想从文件2,打印记录时都COL1和col2 file2中,从文件1匹配COL1。
在这个例子中输出应该是:
File 1 and 2 have an unequal number of records. I would like to print records from File 2, when both Col1 and Col2 in File2 match Col1 from File1. In this example output should be:
1790775963,1932373552,65
感谢您!
A
推荐答案
尝试以下操作:
awk -F',' 'NR==FNR {arr[$1]++; next} (($1 in arr) && ($2 in arr)) {print $0}' file1 file2
输出:
1790775963,1932373552,65
修改
或者更简洁由 sudo_O
awk -F, 'NR==FNR{a[$0];next}($1 in a)&&($2 in a)' file1 file2
这篇关于AWK;通过文件1搜索文件2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文