Is it possible to do a grep with keywords stored in the array.
Here is the possible code snippet... Please correct it
args=("key1" "key2" "key3")
cat file_name |while read line
echo $line | grep -q -w ${args[c]}
done
At the moment, I can search for only one keyword. I would like to search for all the keywords which is stored in args array.
Any suggestion would be highly appreciated.
Thanks, Kiran
This is one way:
Edit:
Based on Pavel Shved's suggestion:
The first version as a one-liner:
Edit2:
Even better than the version using
IFS
:You can use some bash expansion magic to prefix each element with -e and pass each element of the array as a separate pattern. This may avoid some precedence issues where your patterns may interact badly with the | operator:
The downside to this is that you cannot have any spaces in your patterns because that will split the arguments to grep. You cannot put quotes around the above expansion, otherwise you get "-e pattern" as a single argument to grep.
Or with the shell
The command
searches the file for each keyword in an array. It does so by constructing regular expression
word1|word2|word3
that matches any word from the alternatives given (in perl mode).If I there is a way to join array elements into a string, delimiting them with sequence of characters (namely,
\|
), it could be done without perl regexp.perhaps something like this;
not tested!
I tend to use process substitution for everything. It's convenient when combined with
grep
's-f
option:(Depending on the context, you might even want to combine that with
-x
or-w
for awesome effects.)So:
and I get:
I basically write a pseudo-file with one item of the array per line, and then tell
grep
to use each of these lines as a pattern.