需要帮助在bash脚本中使用grep/egrep匹配问题 [英] Need help matching a mattern using grep/egrep in bash scripting
本文介绍了需要帮助在bash脚本中使用grep/egrep匹配问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试匹配给定字符串的所有字符,但这些字符应按照bash脚本给出的顺序进行匹配.
I am trying to match all characters of given string but those characters should match in the order as given to the bash script.
while [[ $# -gt 0 ]]; do
case $1 in
-i)
arg=$2
egrep "*[$arg]*" words.txt
shift ;;
esac
shift
done
$ sh match_the_pattern.sh -i aei words.txt
应返回类似
abstentious
adventitious
sacrilegiousness
如果您注意到,首先匹配a
,然后匹配e
,然后匹配i
,它们都是按顺序排列的.另外,整个单词都经过匹配和过滤.
If you notice, first a
is matched then e
and then i
, all of them are in order. Plus, the whole word is matched and filtered.
推荐答案
更改此内容:
arg=$2
egrep "*[$arg]*" words.txt
对此:
arg=$(sed 's/./.*[&]/g' <<< "$2")
grep "$arg" words.txt
如果这还不是您所需要的,那么请编辑您的问题以阐明您的要求,并提供更真实的示例输入/输出.
If that's not all you need then edit your question to clarify your requirements and provide more truly representative sample input/output.
这篇关于需要帮助在bash脚本中使用grep/egrep匹配问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文