是否可以使用数组中存储的关键字执行 grep 操作?

2024-04-26

是否可以使用存储在数组中的关键字执行 grep 操作。

这是可能的代码片段;我该如何纠正它?

args=("key1" "key2" "key3")

cat file_name |while read line
 echo $line | grep -q -w ${args[c]}
done

目前,我只能搜索一个关键字。我想搜索存储在的所有关键字args array.


args=("key1" "key2" "key3")
pat=$(echo ${args[@]}|tr " " "|")
grep -Eow "$pat" file

或者带壳

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"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否可以使用数组中存储的关键字执行 grep 操作? 的相关文章

随机推荐