【问题标题】:'find' using regex with variables使用带有变量的正则表达式“查找”
【发布时间】:2014-08-24 11:39:02
【问题描述】:

请帮我解决以下问题:

假设是三个文件:

file-aaa.sh 
file-bbb.sh
file-xxx.sh

在 bash 脚本中我有变量

$a=aaa 
$b=bbb

现在,我想执行类似的操作:

find . -name "file-[$a|$b].sh"

并期望在输出中获得两个文件。 我做错了什么?

【问题讨论】:

    标签: regex bash find


    【解决方案1】:

    您可以使用此查找:

    find . -name "file-$a.sh" -o -name "file-$b.sh"
    

    使用-regex 选项将其合并为一个:

    在 OSX 上:

    find -E . -regex ".*file-($a|$b)\.txt"
    

    在 Linux 上:

    find . -regextype posix-extended -regex ".*file-($a|$b)\.txt"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多