sed命令在包含某个字符上一行或者下一行插入内容,sed替换变量内容

1、在包含某个字符的上一行或者下一行插入内容:

[root@RedHat test]# cat testfile 
hello
[root@RedHat test]# sed -i '/hello/i\\up' testfile 
[root@RedHat test]# cat testfile 
up
hello
[root@RedHat test]# sed -i '/hello/a\\down' testfile 
[root@RedHat test]# cat testfile 
up
hello
down
[root@RedHat test]#

假如有两个关键字hello,那么在每一行上面或者下面都插入内容

2、sed替换变量内容:
替换普通字符串:
sed -i ‘s/10/100/g’ testfile 替换成功
替换变量:
sed -i ‘s/10/$num/g’ testfile 替换失败

需要把单引号换成双引号:
sed -i “s/10/$num/g” testfile 替换成功

你可能感兴趣的:(linux)