views:

129

answers:

5

Hello, 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

A: 

perhaps something like this;

cat file_name |while read line
for arg in ${args[@]}
do
echo $line | grep -q -w $arg}
done
done

not tested!

matpol
Well, I need to search for both the keywords at the same time something like ${args[*]} or with egrep with multiple keywords.Any idea ?
Kiran
+1  A: 

The command

( IFS="|" ; grep --perl-regexp "${args[*]}" ) <file_name

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.

Pavel Shved
On my system, it's singular: `--perl-regexp`. Also, useless use of `cat`.
Dennis Williamson
@Dennis, thanks, fixed. By the way, I can argue about cat.
Pavel Shved
`( IFS ... grep ... file_name )` - no need for redirection. Does your `grep` really have an "s" in `--perl-regexp`?
Dennis Williamson
@Dennis, no, it doesn't have "s", I pasted version with typo.
Pavel Shved
+2  A: 

This is one way:

args=("key1" "key2" "key3")
keys=${args[@]/%/\\|}      # result: key1\| key2\| key3\|
keys=${keys// }            # result: key1\|key2\|key3\|
grep "${keys}" file_name 

Edit:

Based on Pavel Shved's suggestion:

( IFS="|"; keys="${args[*]}"; keys="${keys//|/\\|}"; grep "${keys}" file_name )

The first version as a one-liner:

keys=${args[@]/%/\\|}; keys=${keys// }; grep "${keys}" file_name

Edit2:

Even better than the version using IFS:

printf -v keys "%s\\|" "${args[@]}"; grep "${keys}" file_name
Dennis Williamson
A: 
args=("key1" "key2" "key3")
pat=$(echo ${args[@]}|tr " " "|")
grep -Eow "$pat" file

Or with the shell

args=("key1" "key2" "key3")
while read -r line
do
    for i in ${args[@]}
    do
        case "$line" in
            *"$i"*) echo "found: $line";;
        esac
    done
done <"file"
ghostdog74
Thanks a lot.. It worked. :)
Kiran
A: 

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:

$ grep ${args[@]/#/-e } file_name

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.

camh