sed基本用法

-n屏蔽默认输出,默认sed会输出读取文档的全部内容

-r(让sed支持扩展则)

-i(sed直接修改源文件,默认sed只是通过内存临时修改文件,源文件无影响)

sed '1p' /etc/hosts

sed -n '1p' /etc/hosts

sed -n '3,6p' /etc/passwd

sed 'd' /etc/passwd

sed -i '1,4d' test.txt

sed -n '1p;4p' /etc/passwd

sed -n '3p' /etc/passwd

sed '$d' a.txt                //删除文件的最后一行

扫描二维码关注公众号,回复: 2000780 查看本文章

sed '/^$/d' a.txt             //删除所有空行

sed '/^install/d' a.txt    //删除以install开头的行

替换操作的分隔“/”可改用其他字符,如#、&等,便于修改文件路径

sed 's/xml/XML/' a.txt //将每行中第一个xml替换为XML

sed 's/xml/XML/g' a.txt //将所有的xml都替换为XML

sed s#/bin/bash#/sbin/sh#/sbin/sh#' a.txt //将/bin/bash替换为/sbin/sh

sed '4,7s/^/#/' a.txt //将第4~7行注释

sed 's/^#an/an/' a.txt //解除以#an开头的行的注释

删除文件中每行的第二个,最后一个字符

分两次替换操作,第一次替换掉第2个字符,第二次替换最后一个字符

sed 's/.//2;s/.$//' nssw.txt

将文件中每行的第一个、倒数第1个字符互换

每行文本拆分为“第1个字符”、“中间的所有字符”、“倒数第1个字符”三个部分,然后通过操作重排顺序为“3-2-1”;

sed -r 's/^(.)(.*)(.)$/\3\2\1/' nssw.txt

删除文件中的所有数字

sed 's/[0-9]//' nssw.txt

删除所有数字、行首空格的操作如下:

sed -r 's/[0-9]//g;s/^()+//' nssw.txt

为文件中每个大写字母添加括号

使用“()”可实现保留功能,所以可参考下列操作解决:

sed 's/([A-Z])/[\1]/g' nwss.txt

sed多行文本处理

i:在指定的行之前插入文本

a: 在指定的行之后追加文本

c: 替换指定的行

sed '2a xx' a.txt //在第二行后面,追加xx

sed '2i xx' a.txt //在第二行前面,插入xx

sed '2c xx' a.txt //将第二行替换为xx

sed '1c www.test.com' /etc/hostname //使用sed修改主机配置所在行的内容(c 为整行替换)

            

     注意:使用s/旧/新/,使用s替换时,最后需要/结尾

#sed [选项] '条件指令' 文件

选项:

-n 屏蔽默认输出

-r 支持扩展正则

-i 修改源文件

条件:

行号 4 4,5 4~2 4,+10

/正则/

指令:

p 打印

d 删除

s 替换s/旧/新/g

a 追加

i 插入

c 替换行

sed 读

[root@rootroom9pc01 ~]# cat test1.txt 
ffff
ffff
iiii
[root@rootroom9pc01 ~]# sed -i '2r test.txt' test1.txt 
[root@rootroom9pc01 ~]# cat test1.txt 
ffff
ffff
Aerqwer
Adfderflkd
iiii
[root@rootroom9pc01 ~]#

[root@rootroom9pc01 ~]# sed -i '/^i/r test.txt' test1.txt 
[root@rootroom9pc01 ~]# cat test1.txt 
ffff
ffff
Aerqwer
Adfderflkd
iiii
Aerqwer
Adfderflkd

sed 写

[root@rootroom9pc01 ~]# cat test.txt 

test
22test
test
[root@rootroom9pc01 ~]# cat test1.txt
test
[root@rootroom9pc01 ~]# sed -n '2w test1.txt' test.txt
[root@rootroom9pc01 ~]# cat test1.txt
22test
[root@rootroom9pc01 ~]# cat test.txt
test
22test
test

[root@rootroom9pc01 ~]#


sed -n '/^xx/w x.txt'  test.txt   //test.txt为导入的文本 

sed 复制剪切

H :模式空间--追加--保持空间

h:模式空间--覆盖--保持空间        复制

G:保持空间--追加--模式空间

g:保持空间--覆盖--模式空间         粘贴


[root@rootroom9pc01 ~]# sed '2H;$G' test.txt      //加i选项时有效
test
22test
test

                //H:
22test
[root@rootroom9pc01 ~]# sed '2h;$G' test.txt       
test
22test
test
22test   //h:
[root@rootroom9pc01 ~]# 

[root@rootroom9pc01 ~]# cat test.txt   
test
22test
e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3test
[root@rootroom9pc01 ~]# sed '1h;1d;$G' test.txt//把第一行剪切到末尾h:模式空间--覆盖--保持空间1d第一行删除第二行覆盖第一行;G把第一行追加到未尾  -i 时有效
22test
e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3test
test

[root@rootroom9pc01 ~]# 

[root@rootroom9pc01 ~]# cat test.txt
test
22test

e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3test


[root@rootroom9pc01 ~]# sed '1h;2H;1,2d;' test.txt

e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3test


[root@rootroom9pc01 ~]# sed '1h;2H;1,2d;$G' test.txt    //$G追加粘贴
e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3e3test
test
22test
[root@rootroom9pc01 ~]# 


猜你喜欢

转载自blog.csdn.net/weixin_40018205/article/details/80956540
今日推荐