用 Bash 变量替换 sed
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7680504/
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
sed substitution with Bash variables
提问by csta
I am trying to change the values in a text file using sed in a Bash script with the line,
我正在尝试使用 Bash 脚本中的 sed 更改文本文件中的值,
sed 's/draw($prev_number;n_)/draw($number;n_)/g' file.txt > tmp
This will be in a for
loop. Why is it not working?
这将是一个for
循环。为什么它不起作用?
回答by k.parnell
Variables inside '
don't get substituted in Bash. To get string substitution (or interpolation, if you're familiar with Perl) you would need to change it to use double quotes "
instead of the single quotes:
里面的变量'
不会在 Bash 中被替换。要获得字符串替换(或插值,如果您熟悉 Perl),您需要将其更改为使用双引号"
而不是单引号:
$ # Enclose the entire expression in double quotes
$ sed "s/draw($prev_number;n_)/draw($number;n_)/g" file.txt > tmp
$ # Or, concatenate strings with only variables inside double quotes
$ # This would restrict expansion to the relevant portion
$ # and prevent accidental expansion for !, backticks, etc.
$ sed 's/draw('"$prev_number"';n_)/draw('"$number"';n_)/g' file.txt > tmp
$ # A variable cannot contain arbitrary characters
$ # See link in the further reading section for details
$ a='foo
bar'
$ echo 'baz' | sed 's/baz/'"$a"'/g'
sed: -e expression #1, char 9: unterminated `s' command
Further Reading:
进一步阅读:
- Difference between single and double quotes in Bash*
- Is it possible to escape regex metacharacters reliably with sed
- Using different delimiters for sed substitute command
- Unless you need it in a different file you can use the -i flag to change the file in place
- Bash中单引号和双引号的区别*
- 是否可以使用 sed 可靠地转义正则表达式元字符
- 为 sed 替代命令使用不同的分隔符
- 除非您在不同的文件中需要它,否则您可以使用-i 标志来更改文件
回答by Paul Creasey
Variables within single quotes are not expanded, but within double quotes they are. Use double quotes in this case.
单引号内的变量不会扩展,但双引号内的变量会扩展。在这种情况下使用双引号。
sed "s/draw($prev_number;n_)/draw($number;n_)/g" file.txt > tmp
You could also make it work with eval
, but don't do that!!
你也可以让它与 一起工作eval
,但不要那样做!!
回答by Kent
This may help:
这可能有帮助:
sed "s/draw($prev_number;n_)/draw($number;n_)/g"
回答by daemonsl
You can use variables like below. Like here, I wanted to replace hostname
i.e., a system variable in the file. I am looking for string look.me
and replacing that whole line with look.me=<system_name>
您可以使用如下变量。就像这里,我想替换hostname
ie,文件中的一个系统变量。我正在寻找字符串look.me
并将整行替换为look.me=<system_name>
sed -i "s/.*look.me.*/look.me=`hostname`/"
You can also store your system value in another variable and can use that variable for substitution.
您还可以将系统值存储在另一个变量中,并可以使用该变量进行替换。
host_var=
`hostname`
host_var=
`主机名`
sed -i "s/.*look.me.*/look.me=$host_var/"
sed -i "s/.*look.me.*/look.me=$host_var/"
Input file:
输入文件:
look.me=demonic
Output of file (assuming my system name is prod-cfm-frontend-1-usa-central-1
):
文件输出(假设我的系统名称是prod-cfm-frontend-1-usa-central-1
):
look.me=prod-cfm-frontend-1-usa-central-1