是否可以使用存储在数组中的关键字进行 grep。
这是可能的代码片段;我该如何纠正它?
args=("key1" "key2" "key3")
cat file_name |while read line
echo $line | grep -q -w ${args[c]}
done
目前,我只能搜索一个关键字。我想搜索存储在 args 数组中的所有关键字。
最佳答案
args=("key1" "key2" "key3")
pat=$(echo ${args[@]}|tr " " "|")
grep -Eow "$pat" file
或者用 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"
关于bash - 是否可以使用存储在数组中的关键字进行 grep?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2295361/