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

fnvucqvd  于 2023-05-18  发布在  Shell
关注(0)|答案(1)|浏览(142)

此问题已在此处有答案

Need help matching a mattern using grep/egrep in bash scripting(3个答案)
4年前关闭。
所以我需要在给定的单词列表中匹配一个字符串。例如,如果我给予输入像

$ sh match_the_pattern.sh -i aei words.txt

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

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

注意:字符串可能会改变,字典可能会改变。
我的方法:
1.读取输入
1.扫描文件名

  1. SED或GREP组合匹配字符串
    1.输出
    我不知道什么?第三部分。
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是一个接一个的顺序。这就是我想要的。我们要匹配的字符串可能会改变。

ergxz8rk

ergxz8rk1#

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

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

相关问题