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

 

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

 。

 

posted @ 2023-11-17 12:51  小鲨鱼2018  阅读(292)  评论(0编辑  收藏  举报