Linux sed替换字符串

编程入门 行业动态 更新时间:2024-10-25 02:21:09
Linux sed替换字符串 - 有人可以解释这个错误吗?(Linux sed replace a string - Can someone explain this error?)

我有1个文件,希望用sed替换字符串。

A.TXT

#define 1 #define 2

b.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 2

b.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

My expect result in 1.txt:

#define 1 "123 %s abc 3.0 %d <<456: %s>>" #define 2

But I got the error message from console:

sed: -e expression #1, char 27: unterminated `s' command

Could someone help explain the condition for this? thanks.

最满意答案

因为您在sed命令周围使用单引号,所以shell变量${MODEL_NAME}永远不会扩展。 尝试:

sed -i "s/#define 1/#define 1 \"${MODEL_NAME}\"/g" 1.txt

Because 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

更多推荐

本文发布于:2023-07-30 23:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   Linux   sed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!