sed 修改文件
总结
正确的修改进文件命令(替换文件内容):
sed -i "s#machangwei#mcw#g" mcw.txt
正确的修改追加进文件命令(追加文件内容):
sed -i "\$a 8: wo bu hao\!" mcw.txt
sed -i '$a 7: wo hen hao!' mcw.txt
最后一行$a追加内容,加上-i修改到文件;单引号无需转义,双引号需要注意特殊字符转义
只有-i和替换,没有-n和打印,才是我们经常需要的替换。将文件内容替换并写入到文件了
第几行上面插入字符串,但是并不修改原文件,加上-i也不行,使用单双引号也不行
匹配指定行上面插入或追加字符串,会报错,加上-i不行,不加还是不行,
替换文件存在的坑
sed -i "s#machangwei#mcw#g" mcw.txt
只有-i和替换,没有-n和打印,才是我们经常需要的替换。将文件内容替换并写入到文件了。不能添加-n和打印做sed替换
替换实操
首先创建一个测试文件
[machangwei@mxxx01 ~/mcw]$ mkdir mcw03
[machangwei@mxxx01 ~/mcw]$ cd mcw03/
[machangwei@mxxx01 ~/mcw/mcw03]$ vim mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi machangwei
4: 22bc
5: fff abc
6: abc
[machangwei@mxxx01 ~/mcw/mcw03]$ cp mcw.txt mcw.txt.bak
[machangwei@mxxx01 ~/mcw/mcw03]$ ls
mcw.txt mcw.txt.bak
-n却没有打印,不显示替换结果也不改变文件
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -n "s#machangwei#mcw#g" mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi machangwei
4: 22bc
5: fff abc
6: abc
-n没有打印,就是空内容,加上-i就把空内容加到文件里了。文件就空了,内容被删除。sed操作前最好先备份
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -ni "s#machangwei#mcw#g" mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$
[machangwei@mxxx01 ~/mcw/mcw03]$ cp mcw.txt.bak mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -ni "s#machangwei#mcw#g" mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ cp mcw.txt.bak mcw.txt
-n且打印,就显示替换后的那一行,
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -n "s#machangwei#mcw#gp" mcw.txt
3: wo shi mcw
没有-n,替换然后打印。替换的行是重复打印
[machangwei@mxxx01 ~/mcw/mcw03]$ sed "s#machangwei#mcw#gp" mcw.txt
1: ni hao ya
2:
3: wo shi mcw
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
-n加打印p,只显示替换的行,并且显示是替换后的结果
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -n "s#machangwei#mcw#gp" mcw.txt
3: wo shi mcw
-n加打印p,本来就显示替换后的行,加个-i修改文件,那么把显示的替换都的行写入文件,相当于重写文件或者说把显示结果覆盖到原文件
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -ni "s#machangwei#mcw#gp" mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
3: wo shi mcw
[machangwei@mxxx01 ~/mcw/mcw03]$ cp mcw.txt mcw.txt.bak
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -n "s#machangwei#mgp" mcw.txt
sed:-e 表达式 #1,字符 16:未终止的“s”命令
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -n "s#machangwei#mcw#gp" mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
3: wo shi mcw
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt.bak
3: wo shi mcw
-i插入,后面还有个p打印,会将替换掉的行重复写入到文件
[machangwei@mxxx01 ~/mcw/mcw03]$ vim mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ vim mcw.txt.bak
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -n "s#machangwei#mcw#gp" mcw.txt
3: wo shi mcw
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -i "s#machangwei#mcw#gp" mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt.bak
1: ni hao ya
2:
3: wo shi machangwei
4: 22bc
5: fff abc
6: abc
[machangwei@mxxx01 ~/mcw/mcw03]$ cp mcw.txt.bak mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -i "s#machangwei#mcw#gp" mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
只有-i和替换,没有-n和打印,才是我们经常需要的替换。将文件内容替换并写入到文件 了
[machangwei@mxxx01 ~/mcw/mcw03]$ cp mcw.txt.bak mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -i "s#machangwei#mcw#g" mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
[machangwei@mxxx01 ~/mcw/mcw03]$
[machangwei@mxxx01 ~/mcw/mcw03]$ #上面是替换中存在的坑
追加到文件最后一行实操
准备文件
[machangwei@mxxx01 ~/mcw/mcw03]$ #下面是插入和追加到文件
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
最后一行追加内容,不加-i会显示追加后的内容,但是不修改文件。这里是单引号
[machangwei@mxxx01 ~/mcw/mcw03]$ sed '$a 7: wo hen hao!' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
最后一行追加内容,加上-i修改到文件。这里单引号
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -i '$a 7: wo hen hao!' mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
[machangwei@mxxx01 ~/mcw/mcw03]$ sed "\$a 8: wo bu hao!" mcw.txt
-bash: !": event not found
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -i '$a 7: wo hen hao\!' mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ vim mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$
双引号最后一行追加,需要将里面的特殊字符串转义 。不加-i不修改文件
[machangwei@mxxx01 ~/mcw/mcw03]$ sed "\$a 8: wo bu hao\!" mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
[machangwei@mxxx01 ~/mcw/mcw03]$ #双引号里特殊字符会被解析,所以要转义
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
双引号最后一行追加,需要将里面的特殊字符串转义 。加-i修改了原文件
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -i "\$a 8: wo bu hao\!" mcw.txt
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
[machangwei@mxxx01 ~/mcw/mcw03]$ sed "$d" mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
第几行插入字符串,但是并不修改原文件
[machangwei@mxxx01 ~/mcw/mcw03]$ sed '7i 小马过河' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
小马过河
7: wo hen hao!
8: wo bu hao!
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
第几行插入字符串,但是并不修改原文件,加上-i也不行
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -i '7i 小马过河' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
-i 小马过河
7: wo hen hao!
8: wo bu hao!
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
[machangwei@mxxx01 ~/mcw/mcw03]$ sed '7i 小马过河' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
-i i 过河
7: wo hen hao!
8: wo bu hao!
第几行上面插入字符串,但是并不修改原文件,加上-i也不行使用双引号也不行
[machangwei@mxxx01 ~/mcw/mcw03]$ sed "7i 小马过河" mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
小马过河
7: wo hen hao!
8: wo bu hao!
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -i "7i 小马过河" mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
-i 小马过河
7: wo hen hao!
8: wo bu hao!
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
匹配指定行上面插入字符串,但是并不修改原文件,加上-i也不行,使用双引号也不行,
[machangwei@mxxx01 ~/mcw/mcw03]$ sed "/wo he/i 小马过河" mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
-i/wo he/i马过河
7: wo hen hao!
8: wo bu hao!
[machangwei@mxxx01 ~/mcw/mcw03sed '/wo he/i 小马过河' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
小马过河
7: wo hen hao!
8: wo bu hao!
[machangwei@mxxx01 ~/mcw/mcw03]$
[machangwei@mxxx01 ~/mcw/mcw03]$ sed '/wo he/i 小马过河' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
小马过河
7: wo hen hao!
8: wo bu hao!
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
匹配指定行上面插入或追加字符串,会报错,加上-i不行,不加还是不行,
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -i '/wo he/i 小马过河' mcw.txt
> ^C
[machangwei@mxxx01 ~/mcw/mcw03]$ sed '/abc/a 小马过河' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
小马过河
6: abc
小马过河
7: wo hen hao!
8: wo bu hao!
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
[machangwei@mxxx01 ~/mcw/mcw03]$ sed -i '/abc/a 小马过河' mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
[machangwei@mxxx01 ~/mcw/mcw03]$ cat mcw.txt
1: ni hao ya
2:
3: wo shi mcw
4: 22bc
5: fff abc
6: abc
7: wo hen hao!
8: wo bu hao!
符合条件的sed替换,并且排除一部分符合的
排除/ 和/boot的,其它的磁盘挂载都注释掉
# sed -r '/boot|\/ /!s/^UUID(.*)/#UUID\1/g' /etc/fstab # # /etc/fstab # Created by anaconda on Fri Sep 18 12:44:48 2020 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=f48459f1-1f07-4dc3-99e2-f2f93b4519b5 / ext4 defaults,noatime 1 1 UUID=c0024bf0-0ebe-4837-9230-6a70f7b6f27f /boot ext4 defaults,noatime 1 2 #UUID=2a02c224-050e-414d-9039-a6d008c6cab3 /data ext4 defaults,noatime 1 2
执行替换的时候i放到后面才行,放前面语法报错了
# sed -ir '/boot|\/ /!s/^UUID(.*)/#UUID\1/g' /etc/fstab sed: -e expression #1, char 32: invalid reference \1 on `s' command's RHS # sed -ri '/boot|\/ /!s/^UUID(.*)/#UUID\1/g' /etc/fstab #
参考链接:https://www.cnblogs.com/ggzhangxiaochao/p/13131424.html