使用Sed将字符串变量替换为字符串变量

编程入门 行业动态 更新时间:2024-10-21 03:37:48
本文介绍了使用Sed将字符串变量替换为字符串变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个名为 ethernet 的文件,其中包含多行.我已将其中一行保存为名为 old_line 的变量.该变量的内容如下所示:

I have a file called ethernet containing multiple lines. I have saved one of these lines as a variable called old_line. The contents of this variable looks like this:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="2r:11:89:89:9g:ah", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

我创建了另一个名为 new_line 的变量,该变量与 old_line 类似,但在文本中进行了一些修改.

I have created a second variable called new_line that is similar to old_line but with some modifications in the text.

我想使用sed将 old_line 的内容替换为 new_line 的内容.到目前为止,我有以下内容,但不起作用:

I want to substitute the contents of old_line with the contents of new_line using sed. So far I have the following, but it doesn't work:

sed -i "s/${old_line}/${new_line}/g" ethernet

推荐答案

您需要转义oldline,以便它不包含正则表达式特殊字符,幸运的是,可以使用sed完成此操作.

You need to escape your oldline so that it contains no regex special characters, luckily this can be done with sed.

old_line=$(echo "${old_line}" | sed -e 's/[]$.*[\^]/\\&/g' ) sed -i -e "s/${old_line}/${new_line}/g" ethernet

更多推荐

使用Sed将字符串变量替换为字符串变量

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

发布评论

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

>www.elefans.com

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