Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to search for a file with pattern having white space in linux

Tags:

linux

grep

sed

I am struggling hence need your help ( in linux ).

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

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

b) I have lot of others file such as file-1.txt, file-2. txt , file-3.txt.

Problem: I need to search for each line in the output mentioned in section a. above. To illustrate, I want to run equivalent of:

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 above command takes hell lot of time.

Question:

How do I use a single series of command to perform this operation. Whenever I am running command ( as shown below), system only searches for individual words in the line i.e. for aaa and bbb separately 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: command before pipe is bringing the output in space as mentioned in the point a. above.

Any help will be much appreciated.

like image 499
Chandan Prakash Avatar asked Nov 28 '25 23:11

Chandan Prakash


1 Answers

Store all the patterns (aaa bbb etc.) to a file (patterns.txt), one per line, and then

grep -f patterns.txt file-*.txt

will do the job.

like image 130
Hln Avatar answered Nov 30 '25 12:11

Hln



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!