LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Get all lines containing 23 specific words with AWK (http://www.linuxquestions.org/questions/programming-9/get-all-lines-containing-23-specific-words-with-awk-681107/)

cgcamal 11-04-2008 02:56 PM

Get all lines containing 23 specific words with AWK
 

Hi guys,

How can I do an extraction of all lines that contain 23 strings in the file with awk?

I tried with awk 2 words,

Code:

awk '/word1//word2/' source.txt > filtered.txt
but putting the 23 words of interest for me, like below in one line itīs not processed, I get a "bip" sound.
Code:

awk '/word1//word2//.....//word22//word23/' source.txt > filtered.txt

Thanks for any suggestion.

jim mcnamara 11-04-2008 04:37 PM

Put the 23 words in a file - wordfile - one word on each line
Code:

grep -f wordfile inputfile

Disillusionist 11-04-2008 04:56 PM

When I try:
Code:

awk '/word1//word2/' testfile
I get an error message.

When I try:
Code:

awk '/word1/||/word2/' testfile
It works as I expected, matching word1 or word2

If you have commonality in your words, eg:
Code:

awk '/word/&&/6/' testfile
This would match any line that contained both "word" and "6"

Matching:
word6
word06
word16


Would also match:
Why are we drawn to word of the 6?

EDIT:- See Jim's answer!

could also use a file for awk:

awkward_list:
Code:

/word1/
/word2/
/word3/

Code:

awk -f awkward_list testfile

cgcamal 11-05-2008 11:51 AM

Many thanks guys,

Trying your different answers Iīve learned many things. Testing
and testing got what I need.

Thanks again


All times are GMT -5. The time now is 06:36 AM.