使用带有变量的正则表达式“查找" [英] 'find' using regex with variables

查看:38
本文介绍了使用带有变量的正则表达式“查找"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请为我提供以下帮助:

让它成为三个文件:

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

在bash脚本中,我有变量

In a bash script I have variables

$a=aaa 
$b=bbb

现在,我要执行以下操作:

Now, I want to execute something like:

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

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

and expect to get two files in output. What am I doing wrong?

推荐答案

您可以使用以下查找内容:

You can use this find:

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

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

To combine it into one using -regex option:

在OSX上:

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

在Linux上:

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

这篇关于使用带有变量的正则表达式“查找"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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