I found something with grep that works, but am curious to understand why.
I have file1.txt
1
2
3
and file2.txt
1,xx,bb,bb
1,yy,cc,cc
2,xx,dd,dd
2,yy,dd,dd
3,yy,ee,ee
4,aa,ee,ee
5,zz,ee,ee
5,za,ee,ff
x,22,ff,ee
Doing grep "`cat file1.txt `" file2.txt
indeed outputs
1,xx,bb,bb
1,yy,cc,cc
2,xx,dd,dd
2,yy,dd,dd
3,yy,ee,ee
x,22,ff,ee
But why ? I would have thought the it tries to match the full "string" or exat match to several lines at once.
Pointers to follow up readings are welcome.
As described here: http://pubs.opengroup.org/onlinepubs/009695399/utilities/grep.html
The pattern_list's value shall consist of one or more patterns separated by newlines
So a newline is interpreted as delimiter between patterns, and when you quote the output of cat
, the newlines within the file are preserved, and grep uses them to separate the file contents into multiple patterns, one per line. This appears to work the same way as using grep -f
. You can also test this without using cat by adding newlines in your pattern string at the command line:
$ grep "patt1
patt2" file.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With