在awk中,可以使用以下代码来实现grep -wf的功能:
awk 'FNR==NR{a[$0];next}{for(i in a){if($0~i){print}}}' patterns.txt content.txt
其中,patterns.txt是包含需要查找的模式的文件,content.txt是需要进行查找的文件。
解释一下代码:
例如,如果我们有一个名为patterns.txt的文件,其中包含以下几行:
hello world foo bar
并且我们有一个名为content.txt的文件,其中包含以下几行:
hello, this is a test line world, this is another test line foo, bar, baz
运行上述awk命令后,将输出以下内容:
hello, this is a test line world, this is another test line foo, bar, baz
即在content.txt文件中查找并输出可以在patterns.txt文件中找到的所有行。
上一篇:AWK重复替代
下一篇:AWK中去除多余空格