Linux sed替换字符串 - 有人可以解释这个错误吗?(Linux sed replace a string - Can someone explain this error?)
我有1个文件,希望用sed替换字符串。
A.TXT
#define 1 #define 2b.sh
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH MODEL_NAME='123 %s abc 3.0 %d <<456: %s>>' sed -i 's/#define 1/#define 1 \"'${MODEL_NAME}'\"/g' 1.txt我期待的结果是1.txt:
#define 1 "123 %s abc 3.0 %d <<456: %s>>" #define 2但是我收到了来自控制台的错误消息:
sed: -e expression #1, char 27: unterminated `s' command有人可以帮忙解释这个问题吗? 谢谢。
I have 1 file and hope use sed to replace the string.
a.txt
#define 1 #define 2b.sh
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH MODEL_NAME='123 %s abc 3.0 %d <<456: %s>>' sed -i 's/#define 1/#define 1 \"'${MODEL_NAME}'\"/g' 1.txtMy expect result in 1.txt:
#define 1 "123 %s abc 3.0 %d <<456: %s>>" #define 2But I got the error message from console:
sed: -e expression #1, char 27: unterminated `s' commandCould someone help explain the condition for this? thanks.
最满意答案
因为您在sed命令周围使用单引号,所以shell变量${MODEL_NAME}永远不会扩展。 尝试:
sed -i "s/#define 1/#define 1 \"${MODEL_NAME}\"/g" 1.txtBecause you are using single quotes around your sed command, the shell variable ${MODEL_NAME} never gets expanded. Try:
sed -i "s/#define 1/#define 1 \"${MODEL_NAME}\"/g" 1.txt更多推荐
发布评论