How to search for a file with a pattern that has a space in linux

I am struggling, which is why you need your help (on Linux).

a) I have a file with two columns separated by a space (the delimiter is ""). Infact, I run a series of piped commands on the command line that give me the output as above.

aaa bbb
ccc ddd
fff ggg
ccc nnn
fff kkk # there are approx 20,000 such rows.

b) I have many other files like file-1.txt, file-2. txt, file-3.txt.

Problem: I need to find every line in the output given in section a. higher. To illustrate, I want to run the equivalent:

grep 'aaa bbb' file-1 txt file-2.txt file-3 txt 
grep 'ccc ddd' file-1 txt file-2.txt file-3 txt
......
20,000 times
.......

      

But the above command takes a long time.

Question:

How to use one command to perform this operation. Whenever I run the command (as shown below), the system only looks for single words in the string, for example for aaa and bbb, and gives me wrong output.

eg:

cat < filename > | cut -d "," -f1,2 | xargs -I {} sed '{}' file-1.txt

      

or using grep instead of sed ....

NOTE: the command before the pipe outputs the output in space, as indicated in point a. higher.

Any help would be much appreciated.

+3


source to share


2 answers


Store all templates ( aaa bbb

etc.) in a file ( patterns.txt

), one per line, and then

grep -f patterns.txt file-*.txt

      



will do the job.

+3


source


Expanding on Hin's answer, you can pattern and grep on one line using bash process replacement:

grep -F -f <(cut -d, -f1,2 filename) file-*.txt

      



I am assuming that the patterns you create are fixed strings and not regular expressions, so the option -F

0


source







All Articles