I have an input file Input.txt
with the following sample keywords:
One
Two
I also have a file Text.txt
to search such as:
Bla Bla
Two more Bla
I want to print grep Keyword followed by a match if the grep finds a match.
The desired output:
Two:
========
Two more Bla
########
With awk you could do something like this:
$ 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
In a more readable format it would be:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.