用变量替换文本
我想用变量替换该值。由于 sed 中使用了单引号,以下内容不起作用。
#!/bin/sh
set myvarM='pqr'
sed 's/:P03:M15/:P02:M1$myvarM/' mychange.txt > new_mychange.txt
我可以更改 sed 命令还是应该使用其他命令?
I want to replace the value with a variable. The following does not work because of the single quote used in sed.
#!/bin/sh
set myvarM='pqr'
sed 's/:P03:M15/:P02:M1$myvarM/' mychange.txt > new_mychange.txt
Can I change the sed command or should I use something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
顺便说一句,要进行就地替换(即不创建新文件,而是更改原始文件),请执行以下操作:
这表示“使用空白作为增量后缀” - 即写出与输入相同的文件名
Incidentally, to make the replacement in-place (ie not create a new file, but alter the original file), do this:
This says "use a blank as the increment suffix" - ie write out the same filename as the input
请注意,在 bourne 类型 shell (“/bin/sh”) 中,
set
命令设置位置参数。您的第一行将$1
设置为值myvarM='pqr'
—— myvarM 变量继续未设置。Note that in a bourne-type shell ("/bin/sh") the
set
command sets the positional parameters. Your first line sets$1
to the valuemyvarM='pqr'
-- the myvarM variable continues to be unset.