gpt4 book ai didi

linux - 如果 grep 找到匹配项,则打印 grep 关键字

转载 作者:太空宇宙 更新时间:2023-11-04 05:20:45 25 4
gpt4 key购买 nike

我有一个输入文件 Input.txt ,其中包含以下示例关键字:

One 
Two

我还有一个要搜索的文件 Text.txt,例如:

Bla Bla
Two more Bla

如果 grep 找到匹配项,我想打印 grep 关键字,后跟匹配项。

所需的输出:

Two:
========
Two more Bla
########

最佳答案

使用 awk 你可以做这样的事情:

$ awk 'NR==FNR{a[FNR]=$0;next}{for(i in a){if($0~a[i])print a[i]":\n========\n"$0}}' input.txt test.txt 
Two:
========
Two more Bla

如果采用更易读的格式,则为:

awk 'NR == FNR { 
a[FNR] = $0
next
}
{
for(i in a) {
if ($0 ~ a[i]) {
print a[i] ":\n========\n" $0
}
}
}' input.txt test.txt

关于linux - 如果 grep 找到匹配项,则打印 grep 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42191101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com