Linux 中 sed命令替换 带有 斜杠的变量

发布时间 2023-11-17 12:58:49作者: 小鲨鱼2018

 

001、测试(错误做法)

(base) [root@pc1 test]# ls
a.txt
(base) [root@pc1 test]# cat a.txt                             ## 测试数据
1
2
3
4
5
6
7
8
9
10
(base) [root@pc1 test]# sed "s/5/$PWD/" a.txt               ## 直接替换报错
sed: -e expression #1, char 6: unknown option to `s'

 

002、正确做法

(base) [root@pc1 test]# ls
a.txt
(base) [root@pc1 test]# cat a.txt                              ## 测试数据
1
2
3
4
5
6
7
8
9
10
(base) [root@pc1 test]# sed "s#5#$PWD#" a.txt                ## 将替换的斜杠转换为#号
1
2
3
4
/home/test
6
7
8
9
10

 。