我有一个文件(file_1),我想通读并grep来自另一个文件(file_2)的每一行,但它应该只匹配该文件的第一列。
file_1
1
2
78
GL.1234
22
file_2
#blahblah hello this is some file
1 this is still some file 345
1 also still a 12 file
78 blah blah blah
22 oh my gosh, still a file!
GL.1234 hey guys, it's me. just being a file
2 i think that's it.
输出
1 this is still some file 345
1 also still a 12 file
2 i think that's it.
22 oh my gosh, still a file!
78 blah blah blah
GL.1234 hey guys, it's me. just being a file
我试过了:
cat file_1.txt | while read line; do awk -v line = $line '{if ($1 == line) print $0;}' < file_2.txt > output.txt; done
和
cat file_1.txt | while read line; do grep -E '$line\b' < file_2.txt > output.txt; done
答案 0 :(得分:4)
查看您的脚本似乎可以在一个awk
中完成:
awk 'NR==FNR{seen[$1]; next} $1 in seen' file1 file2
<强>输出:强>
1 this is still some file 345
1 also still a 12 file
78 blah blah blah
22 oh my gosh, still a file!
GL.1234 hey guys, it's me. just being a file
2 i think that's it.
基本上,我们先扫描file
并将第一列存储在关联数组seen
中。稍后我们检查此数组中是否存在file2
的column1并打印记录。