Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep inner working (lines with values given by other file)?

Tags:

grep

bash

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.

like image 334
user3484690 Avatar asked Oct 21 '22 11:10

user3484690


1 Answers

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 
like image 164
Josh Jolly Avatar answered Oct 27 '22 10:10

Josh Jolly