需要帮助,在bash脚本中使用egrep匹配模式[SED / GREP]

时间:2019-04-22 18:05:00

标签: bash shell scripting grep

所以我需要匹配给定单词列表中的字符串。 例如,如果我输入类似

$ sh match_the_pattern.sh -i aei words.txt

它应按顺序匹配字符“ a”,“ e”和“ i”。

字符串中的字母必须按该顺序出现在单词中,但其他字符也可能出现在它们之间。

注意:字符串可能会更改,字典可能会更改。

我的方法:

  1. 阅读输入内容
  2. 扫描文件名
  3. SED或GREP组合以匹配字符串
  4. 输出

我不知道什么?第三部分。

while [[ $# -gt 0 ]]; do
  case $1 in
       -i)
    arg=$2
    egrep "*[$argument]*" $name
    shift ;;
  esac
  shift
done

什么没用

a+.*e+.*i+.*

sh match_the_pattern.sh -i aeiou words.txt

adventitious
adventitiousness
sacrilegious
abstemious
sacrilegiousness

如果您注意到,a,e,i,o,u依次排列。那就是我想要的。 我们要匹配的字符串可能会更改。

1 个答案:

答案 0 :(得分:1)

假设$argument仅应包含字母;使用sed生成模式,使用grep匹配单词:

grep "$(sed 's/./&.*/g' <<< "$argument")" "$name"