How do I use grep to search for lines containing all words in a list with no specific order in a text file? [duplicate]

How do I use grep to search for lines containing all words in a list with no specific order in a text file? [duplicate]



This question already has an answer here:



How can we use grep to get line(s) containing a group of words, while the order of words is not important, although line(s) should contain all words mentioned in search?


grep



I have done that with phasing the search (with piping, saving the outputs in temporary files, and searching again), but I'd like to know if I can do that in one attempt.



Here is a sample; I want to search in the lines below for lines containing sample, words, list:


it's a sample test file for a list of words.
list of words without a specific order, it's a sample.
line with no search keyword here.
list the sample files.
something completely different.



And get this result:


it's a sample test file for a list of words.
list of words without a specific order, it's a sample.



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





Are the sentences on separate lines, can OP confirm this?
– George Udosen
Aug 23 at 14:20





This grep -e words -e sample -e list newfile works as well
– George Udosen
Aug 23 at 14:24


grep -e words -e sample -e list newfile





Hi George. Yes, each sentence is on a separate line.
– arash deilami
Aug 23 at 14:27





Probably awk better fits here.
– PerlDuck
Aug 23 at 14:34


awk





@PerlDuck I agree, much easier. awk '(/sample/ && /words/ && /list/)' samplefile
– Terrance
Aug 23 at 14:43



awk '(/sample/ && /words/ && /list/)' samplefile




2 Answers
2



The easy way is with multiple calls to grep:


grep


grep sample testfile.txt | grep words | grep list



A demonstration:


echo -e "it's a sample test file for a list of words.nlist of words without a specific order, it's a sample. nline with no search keyword here. nlist the sample words. nsomething completely different." | grep sample | grep words | grep list
it's a sample test file for a list of words.
list of words without a specific order, it's a sample.
list the sample words.





yes, this works as I've mentioned, but i'm looking for a one-line-solution. Although sometimes it's better to do things the easy way. thanks :)
– arash deilami
Aug 23 at 18:11





grep -e also selects lines which have only two words (not all the words). Maybe I should try awk instead of grep, it works fine. Thank you all
– arash deilami
Aug 23 at 18:18




You can use lookaheads with Perl-regex (-P):


-P


grep -P '(?=.*list)(?=.*words)(?=.*sample)'



Example:


echo "it's a sample test file for a list of words.
list of words without a specific order, it's a sample.
line with no search keyword here.
list the sample words.
sample line with only two of the keywords inside.
something completely different."
| grep -P '(?=.*list)(?=.*words)(?=.*sample)'

it's a sample test file for a list of words.
list of words without a specific order, it's a sample.
list the sample words.



(via)



With agrep (sudo apt install agrep) you can chain multiple patterns:


agrep


sudo apt install agrep


agrep "sample;words;list"



(via)

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế