简体   繁体   English

在两个文件之一中基于模式进行匹配-Linux

[英]Matching based on patern in one of two files - Linux

I have two files of different formats and number of records. 我有两个不同格式和记录数的文件。 I'd like to search for lines in File2 based on the records in File1 and save the entire matched line(s) from File2 in a new file. 我想根据File1中的记录搜索File2中的行,并将File2中整个匹配的行保存到一个新文件中。 The file examples are below: 文件示例如下:

Input File 1 输入文件1

991888
16889000
567899
....
....

Input File 2 输入文件2

16889000    xxxx    yyyy
687398793
5778
567899      aaaa    bbbb
999178
.....

Output file 输出文件

16889000    xxxx    yyyy
567899      aaaa    bbbb

I've tried playing with various grep options, but no luck. 我试过各种grep选项,但没有运气。 Any help to solve this using out of the box tools would be helpful. 使用即用型工具解决此问题的任何帮助都将有所帮助。

Thanks! 谢谢!

awk 'FNR==NR {if (NF > 0) a[FNR""] = $0; next}; {for (x in a) {if ($1 ~ a[x]) {print}}}' input_file1.txt input_file2.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM