【问题标题】:Shell script find command for matching file names with one or the other word用于将文件名与一个或另一个单词匹配的 Shell 脚本查找命令
【发布时间】:2017-07-27 06:39:11
【问题描述】:

我正在寻找 bash shell 脚本的命令来查找目录中名称以 WordA 或 WordB 开头并以数字结尾的文件列表。

我有这个带有WordA 的命令,并用WordB 复制相同的代码。

find /log/ -name 'WordA*[[:digit:]]'

我尝试将 Or 条件置于各种格式中,例如 (WordA|WordB)[WordA|WordB][(WordA)|(WordB)][(WordA)||(WordB)] 以匹配 WordAWordB 后跟数字。

它们都不起作用。

【问题讨论】:

  • 只需运行两个查找命令...find /log/ -name 'WordA*[[:digit:]]' 然后运行find /log/ -name 'WordB*[[:digit:]]'
  • 为简单起见,在问题中提到了 2。实际上,我必须匹配4个左右的单词。考虑到维护的角度,不想粘贴 4 次。
  • @bjskishore123:看看我的回答是否有帮助
  • @Inian:有帮助,但在busybox上不起作用

标签: linux bash shell ubuntu-16.04 busybox


【解决方案1】:

您需要使用find命令中支持的-regextype,在使用posix-extended类型的情况下进行

-regextype type
      Changes the regular expression syntax understood by -regex and -iregex 
      tests which occur later on the command line.  Currently-implemented  
      types  are  emacs (this is the default), posix-awk, posix-basic, 
      posix-egrep and posix-extended.

所以你的命令应该是

find /log/ -regextype posix-extended -regex './(WordA|WordB)([[:digit:]]+)$'

【讨论】:

    【解决方案2】:

    在查找手册页的标题表达式下,您会找到(不是双关语)以下内容

    运营商

    运算符将表达式中的其他项连接在一起。 它们包括例如 -o(表示逻辑或)和 -a(表示 逻辑与)。如果缺少运算符,则假定为 -a。

    因此,您的问题的答案似乎是

     find /log/ -name 'WordA*[[:digit:]]' -o 'WordB*[[:digit:]]'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 2015-06-21
      • 2011-06-03
      • 1970-01-01
      • 1970-01-01
      • 2020-09-20
      • 1970-01-01
      相关资源
      最近更新 更多