[英]How can I replace a line of text in a file with a single command when the line contains a variable?
I have a script called script.sh which has the contents我有一个名为script.sh的脚本,其中包含内容
export HELLO=$WORLD
I want to change it using a single command to我想使用单个命令将其更改为
export HELLO=${WORLD:2:3}
I'm attempting to use the sed command but i'm new to linux and can't quite get it right.我正在尝试使用 sed 命令,但我是 linux 的新手,不能完全正确。 Here's what I have
这就是我所拥有的
sed -i 's/export HELLO=$WORLD/export HELLO=${WORLD:2:3}' script.sh
How should this line be written to replace the text correctly?应该如何编写此行以正确替换文本?
You just missed a /
at the very end:你只是在最后错过了一个
/
:
sed -i 's/export HELLO=$WORLD/export HELLO=${WORLD:2:3}/' script.sh
^
this / is missing
The $ is a special symbol that means end of line, also as @yvesonline points out you are missing the trailing /. $ 是一个特殊符号,表示行尾,正如@yvesonline 指出的那样,您缺少尾随 /。 This is how I would do it:
我会这样做:
sed -i 's/\(export HELLO=\$\)\(WORLD\)/\1{\2:2:3}/' script.sh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.