Issue
My task
I have a file A.txt with the following content.
aijdish uhuih
buh iiu hhuih
zhuh hiu
d uhiuhg ui
...
I want to select lines with these words aijdish
, d
, buh
...
I only know that I can:
cat A.txt | grep "aijdish" > temp.txt
cat A.txt | grep "d" >> temp.txt
cat A.txt | grep "buh" >> temp.txt
...
But I have several thousands of words need to select this time, how can I do this under bash?
Solution
Since you have many words you want to look for I suggest putting the pattern into a file and use greps -f
option:
$ cat grep-pattern.txt
aijdish
buh
d
$ grep -f grep-pattern.txt inputfile
aijdish uhuih
buh iiu hhuih
d uhiuhg ui
But if you have words like d you might want to add the -w
option to match only whole words and not parts of words.
grep -wf grep-pattern.txt inputfile
Answered By - ahilsend Answer Checked By - Marie Seifert (WPSolving Admin)