开发者

Is it possible to do a grep with keywords stored in the array?

Is it possible to do a grep with keywords stored in the array.

Here is the possible code snippet; how can I 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.


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"


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.


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


I tend to use process substitution for everything. It's convenient when combined with grep's -f option:

Obtain patterns from FILE, one per line.

(Depending on the context, you might even want to combine that with -F, -x or -w, etc., for awesome effects.)

So:

#! /usr/bin/env bash

t=(8 12 24)

seq 30 | grep -f <(printf '%s\n' "${t[@]}")

and I get:

8
12
18
24
28

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.


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜