使用sed中的美元符号进行变量替换和字符转换 [英] Using dollar sign in sed for both variable replacement and character
问题描述
我尝试使用sed更改名为convergence.gnu的文件中的行
我有一个名为lafila
的变量,它是一个文件名
I try to use sed to change a line in a file named convergence.gnu
I have a variable named lafila
, which is a file name
目前,我可以这样做:
lafila="nGas060.dat"
sed -i "6s/.*/plot \"$lafila\" using 1:2 with l/" convergence.gnu
这可以将我的convergence.gnu文件的第六行正确更改为:
This changes correctly the sixth line of my convergence.gnu file to:
plot "nGas062.dat" using 1:2 with l
但是,现在我想在替换的行中包含一个美元符号来代替:
However, now I want to include a dollar sign in the replaced line to obtain instead:
plot "nGas062.dat" using ($1/1000):2 with l
您能告诉我sed命令中要进行哪些更改吗?如果我逃脱了一个美元符号,它将无法正常工作.双倍的美元也不起作用.
Can you tell me what to change in my sed command? If I escape a dollar sign it does not work properly. Double dollars also don't work.
推荐答案
使用单引号:
sed -i '6s/.*/plot "'$lafila'" using ($1\/1000):2 with l/' convergence.gnu
单引号保护双引号,并且$
在其内部不解释.但是,您确实需要转义/
.
Single quotes protect double quotes and $
is not interpreted inside them. However, you do need to escape /
.
另请参阅:
这篇关于使用sed中的美元符号进行变量替换和字符转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!