需要帮助在 bash 脚本中使用 grep/egrep 匹配问题 [英] Need help matching a mattern using grep/egrep in bash scripting

查看:18
本文介绍了需要帮助在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆