BASH:如何将变量放入正则表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5713277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BASH: how to put variable inside regex?
提问by Alessio
i'm trying to get working the following code:
我正在尝试使用以下代码:
searchfile="availables.txt"
read searchterm
grep_params="-i ^.*${searchterm}.*;.*$' $searchfile"
egrep $grep_params
which should echo all lines beginning with the $searchterm and followed by ";". But if the searchterm contains spaces it doesn't work (eg: "black eyed peas"), it gives me the following output:
它应该回显所有以 $searchterm 开头并后跟“;”的行。但是,如果搜索词包含空格,则它不起作用(例如:“黑眼豆豆”),它会给我以下输出:
egrep: eyed: No such file or directory
egrep: peas.*;.*$": No such file or directory
egrep: "availables.txt": No such file or directory
回答by bash-o-logist
Just Bash
只是重击
searchfile="file"
read searchterm
shopt -s nocasematch
while read -r line
do
case "$line" in
*"$searchterm"*";"* ) echo "$line";;
esac
done < "$searchfile"
回答by Draco Ater
You need to control word splitting here. That is done through arrays. See http://mywiki.wooledge.org/WordSplitting
您需要在此处控制分词。这是通过数组完成的。见http://mywiki.wooledge.org/WordSplitting
searchfile="availables.txt"
read searchterm
grep_params=(-i "^.*${searchterm}.*;.*$" $searchfile)
egrep "${grep_params[@]}"
But don't use egrep- use grep -Einstead, as the first is deprecated.
But I would have changed your code like that:
但不要使用egrep-grep -E代替,因为第一个已被弃用。但我会像这样改变你的代码:
searchfile="availables.txt"
read searchterm
grep_params="^.*${searchterm}.*;.*$"
grep -E -i "$grep_params" $searchfile
回答by stema
As this is s regex, try to replace the " " with \s. This stands for a whitespace character.
由于这是正则表达式,请尝试将“”替换为\s. 这代表空白字符。
"black\seyed\speas"
“黑眼珠\豌豆”
回答by dogbane
Alternative way is to pass the params through xargs:
另一种方法是通过 xargs 传递参数:
searchfile="availables.txt"
read searchterm
grep_params="-i '^.*${searchterm}.*;.*$' $searchfile"
echo "$grep_params" | xargs egrep
回答by static_rtti
The code you're looking for is something like:
您正在寻找的代码类似于:
searchfile="availables.txt"
read searchterm
regex="${searchterm}"'.*;'
egrep "${grep_params}" "${searchfile}"
Note that I simplified your regex (I hope I got it right!).
请注意,我简化了您的正则表达式(我希望我做对了!)。
However, as Ignacio Vazquez-Abrar noted, this will fail in complex cases.
然而,正如 Ignacio Vazquez-Abrar 所指出的,这在复杂的情况下会失败。
回答by mmrtnt
I got here by searching for "bash variable in a regex"
我通过搜索“正则表达式中的 bash 变量”来到这里
I solved it by changing the regex delimiters from "/" to "+"
我通过将正则表达式分隔符从“/”更改为“+”来解决它
Even though it has nothing to do with egrep, I'm adding my solution for other people who arrive from similar searches:
尽管它与 egrep 无关,但我正在为来自类似搜索的其他人添加我的解决方案:
SYBASELOG="/opt/sybase/ASE-12_5/install/SYBASE.log"
MAILBODY="Some text and then the replacement placeholder:
[MSGFILE]
and some more text"
# Some proecessing...
MAILBODY=`echo "${MAILBODY}" | sed -e "s+\[MSGFILE\]+"${SYBASELOG}"+"`
And yes, I see now that it had little to do with bash and everything to do with the slashes in the log file variable. D'oh!
是的,我现在看到它与 bash 无关,而与日志文件变量中的斜杠有关。哦!

