Newbie question -- any help very much appreciated: I want to be able to get grep (or whatever else would work) to return not only matching lines, but also the original input string:
An example may help: Suppose I have two files data1.txt and data2.txt:
data1.txt
Hello my name is foo.
What is your name?
Shall we meet at the bar?
Perhaps they will have food.
data2.txt
foo bar foo bar
barbarbarbar
xxxxx
yyyyy
foofoofoo
The command:
grep "foo" *.txt > test.out
returns:
data1.txt:Hello my name is foo.
data1.txt:Perha ps they will have food.
data2.txt:foo bar foo bar
data2.txt:foofo ofoo
I would like the command to also include, at the beginning of the line, my original search string (in this case "foo"). Is this possible using grep or perhaps awk, or, something else?
Thanks in advance for your help.
An example may help: Suppose I have two files data1.txt and data2.txt:
data1.txt
Hello my name is foo.
What is your name?
Shall we meet at the bar?
Perhaps they will have food.
data2.txt
foo bar foo bar
barbarbarbar
xxxxx
yyyyy
foofoofoo
The command:
grep "foo" *.txt > test.out
returns:
data1.txt:Hello my name is foo.
data1.txt:Perha ps they will have food.
data2.txt:foo bar foo bar
data2.txt:foofo ofoo
I would like the command to also include, at the beginning of the line, my original search string (in this case "foo"). Is this possible using grep or perhaps awk, or, something else?
Thanks in advance for your help.
Comment