运维界的卡乐咪

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

功能说明:

sed  stream editor  增删改查  过滤  取行

查看sed版本:sed --version

语法格式:

sed [options] [sed-commands] [input-file]

sed    选项         sed命令     输入文件

sed语句

input-file 可以是文本也可以是标准输入

sed软件

 sed选项:

-n取消默认输出

-i修改文件

-f后接sed脚本文件名

-r使用扩展正则表达式

-e执行多条sed命令

 

sed软件执行流程

 

选项说明

[options] 选项

a追加

i插入

[sed-commands] sed 命令

指定执行的地址范围

n1[,n2]{sed-commands}

10{sed-commands}对第十行执行sed命令

10,20{sed-commands}对第十行到第二十行执行sed命令

10,+20{sed-commands}对第十行到第三十行执行sed命令

1~1{sed-commands} 1 3 5 7 9

[root@localhost ~]# seq 10 | sed -n '1~2p'
1
3
5
7
9


2~2{sed-commands} 2 4 6 8 10

[root@localhost ~]# seq 10 | sed -n '2~2p'
2
4
6
8
10

 

10,${sed-commands}从第10行到最后一行

 

/aaa/{sed-commands}匹配aaa执行sed

/aaa/,/bbb/{sed-commands}从aaa到bbb执行sed命令

/aaa/,${sed-commands}从aaa到最后一行

10,/bbb/{sed-commands}从第10行到包含bbb那行

/aaa/,10{sed-commands}从aaa开始那行到第十行

/aaabbb/,+20{sed-commands}从aaabb到其后的20行

 

 

实例:

1.增删改查

1.1增

单行增加

a在第三行之后追加

[root@localhost ~]# sed '3a 16,ceshi,CSO' person.txt
11,zhangsan,CEO
12,lisi,CTO
13,wangwu,COO
16,ceshi,CSO
14,ssk,CFO
15,feiyuxing,CIO
i在第三行之前插入

[root@localhost ~]# sed '3i 17,cccc,CSO' person.txt
11,zhangsan,CEO
12,lisi,CTO
17,cccc,CSO
13,wangwu,COO
14,ssk,CFO
15,feiyuxing,CIO

多行增加

[root@localhost ~]# sed '3a 18,duohang,CCO\n19,duohang1,CTT' person.txt
11,zhangsan,CEO
12,lisi,CTO
13,wangwu,COO
18,duohang,CCO
19,duohang1,CTT
14,ssk,CFO
15,feiyuxing,CIO
不加\n可以以\结尾回车写第二行

[root@localhost ~]# sed '3a 18,duoha.CSO\
> 20,duoha1,CCO' person.txt
11,zhangsan,CEO
12,lisi,CTO
13,wangwu,COO
18,duoha.CSO
20,duoha1,CCO
14,ssk,CFO
15,feiyuxing,CIO

实例:优化SSH配置 /etc/ssh/sshd_config

sed -i '16a Port 22\nPermitRootLogin no\nPermitEmptyPasswords no\nUseDNS no\nGSSAPIAuthentication no' /etc/ssh/sshd_config

2.删

d删除第二行

[root@localhost ~]# sed '2d' person.txt
11,zhangsan,CEO
13,wangwu,COO
14,ssk,CFO
15,feiyuxing,CIO


删除2-5行
[root@localhost ~]# sed '2,5d' person.txt
11,zhangsan,CEO
案例:打印文件内如但不包含zhangsan

[root@localhost ~]# sed '/zhangsan/d' person.txt
12,lisi,CTO
13,wangwu,COO
14,ssk,CFO
15,feiyuxing,CIO

3.改

c替换指定的行

[root@localhost ~]# cat person.txt
11,zhangsan,CEO
12,lisi,CTO
13,wangwu,COO
14,ssk,CFO
15,feiyuxing,CIO
[root@localhost ~]# sed '2c 12,llll.tttt' person.txt
11,zhangsan,CEO
12,llll.tttt
13,wangwu,COO
14,ssk,CFO
15,feiyuxing,CIO

s替换指定字符串  g是s命令的替换标志-全局替换标志

不加-i临时修改,数据在缓存里,加-i将修改写入文件

sed ‘s#被替换的字符串#替换的字符串 #g’ person.txt  =======【sed -i 's/ / /g' person.txt 官方以/为定界符】

g

[root@localhost ~]# cat person.txt
11,zhangsan,CEO
12,lisi,CTO
13,wangwu,COO
14,ssk,CFO
15,feiyuxing,CIO
[root@localhost ~]# sed 's#lisi#NB#g' person.txt
11,zhangsan,CEO
12,NB,CTO
13,wangwu,COO
14,ssk,CFO
15,feiyuxing,CIO

sed -i ‘s#被替换的字符串#替换的字符串 #g’ person.txt

[root@localhost ~]# sed -i 's#lisi#NB#g' person.txt
[root@localhost ~]# cat person.txt
11,zhangsan,CEO
12,NB,CTO
13,wangwu,COO
14,ssk,CFO
15,feiyuxing,CIO
实例:指定行修改配置文件,将selinux关闭

修改指定内容

[root@localhost ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
[root@localhost ~]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

 修改指定行内容

sed -i '7s#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

p 打印输出指定内容,由于sed有默认输出,需要与-n选项联用

-n 取消sed默认输出

[root@localhost ~]# cat person.txt
a
b
a
[root@localhost ~]# sed '2p' person.txt
a
b
b
a
[root@localhost ~]# sed -n '2p' person.txt
b

sed备份文件:sed -i.ori 's#a#zzz#g' person.txt

 

[root@localhost ~]# cat person.txt
a
b
a
[root@localhost ~]# sed -i.ori 's#a#zzz#g' person.txt
[root@localhost ~]# cat person.txt
zzz
b
zzz
[root@localhost ~]# cat person.txt.ori
a
b
a

另存文件

w 另存文件

sed ' w output.txt' person.txt

[root@localhost ~]# sed 'w output.txt' person.txt
zzz
b
zzz
[root@localhost ~]# cat output.txt
zzz
b
zzz
修改并另存为

[root@localhost ~]# sed 's#b#NB#g w output.txt' person.txt
zzz
NB
zzz

替换命令详解

sed -i 's#[被替换的字符串]#[替换后的字符串]#g' person.txt

sed '[address-range|pattern=rang] s#original-string#replacement-string#[substitute-flags]'  [input file]

sed '[地址范围] [模式范围] s#[被替换的字符串]#[替换后的字符串]#[替换标志]' [输入文件]

替换标志分为:g全局标志、数字标志1,2,3...、p打印标志、w写标志、i忽略大小写、e执行命令标志。

g全局标志 

Ms# # #Ng
Ms 对M行操作 如果没有g标志只匹配第一列进行处理,如果有g会对整行处理

Ng 从第N处/列开始往后全部替换

Ms Ng一起使用,对第M行第N处匹配替换

 

[root@localhost ~]# cat num.txt
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
[root@localhost ~]# sed '2s#1#0#' num.txt
1 1 1 1 1
0 1 1 1 1
1 1 1 1 1
1 1 1 1 1
[root@localhost ~]# sed '2s#1#0#g' num.txt
1 1 1 1 1
0 0 0 0 0
1 1 1 1 1
1 1 1 1 1
[root@localhost ~]# sed '2s#1#0#2g' num.txt
1 1 1 1 1
1 0 0 0 0
1 1 1 1 1
1 1 1 1 1

[root@localhost ~]# sed '2s#1#0#2' num.txt
1 1 1 1 1
1 0 1 1 1
1 1 1 1 1
1 1 1 1 1

数字标志1,2,3...

X只对X处/列替换

1<X<512

p打印标志

[root@localhost ~]# sed -n '2s#1#0#2p' num.txt
1 0 1 1 1

w写标志

替换后将内容写入person.txt

[root@localhost ~]# sed 's#1#222#w person.txt' num.txt
222 1 1 1 1
222 1 1 1 1
222 1 1 1 1
222 1 1 1 1
[root@localhost ~]# cat person.txt
222 1 1 1 1
222 1 1 1 1
222 1 1 1 1
222 1 1 1 1
[root@localhost ~]# sed 's#1#222#;w person.txt' num.txt   #分号的作用在一条命令上执行两条不同命令;有分号分隔代表后面的w是命令,没有分号是标志
222 1 1 1 1
222 1 1 1 1
222 1 1 1 1
222 1 1 1 1

i忽略大小写

忽略大小写进行替换

[root@localhost ~]# cat person.txt
AAA
vvbB
BBBb
CCCE
DDDdd
cccs
[root@localhost ~]# sed 's#bbbb#nbnb#g' person.txt
AAA
vvbB
BBBb
CCCE
DDDdd
cccs
[root@localhost ~]# sed 's#bbbb#nbnb#i' person.txt
AAA
vvbB
nbnb
CCCE
DDDdd
cccs

e执行命令标志

将模式空间的任何内容当做bash命令执行

-rw-r--r-- 1 root root 963 7月  12 2019 /etc/passwd
[root@localhost ~]# cat file.txt
/etc/my.cnf
/etc/passwd
[root@localhost ~]# sed 's#^#ls -lh #e' file.txt
-rwxr-xr-x 1 root root 4.7K 7月  13 2019 /etc/my.cnf
-rw-r--r-- 1 root root 963 7月  12 2019 /etc/passwd

其他替换标志

\l 在替换字符中使用\l标志时,它会把紧跟在其后面的字符当做小写字符来处理。

[root@localhost ~]# sed -n 's#vvbB#A\lAA#p' person.txt
AaA

\L 在替换字符中使用\L标志时,它会把后面所有的字符都当做小写字符来处理

[root@localhost ~]# sed -n 's#BBBb#AAA\Lshdnf#p' person.txt
AAAshdnf

\u在替换字符中使用\u标志时,它会把紧跟在其后面的字符当做大写字符来处理

[root@localhost ~]# sed -n 's#BBBb#AAA\ushdnf#p' person.txt
AAAShdnf

\U在替换字符中使用\U标志时,它会把后面所有的字符都当做大写字符来处理

[root@localhost ~]# sed -n 's#BBBb#AAA\Ushdnf#p' person.txt
AAASHDNF

\E需要和\U或\L一起使用,它将关闭\U或\L的功能

特殊符号 = 取行号

[root@localhost ~]# sed '=' person.txt
1
aaaaaa
2
BBBBB
3
ccDD

一条sed语句执行多条sed命令

实例:删除第三行到末尾的数字,并把101替换为100

[root@localhost ~]# cat person.txt
101 aaaaaa
102 BBBBB
103 ccDD
104 dddds
105 wetsdg

方法一:
[root@localhost ~]# sed '3,$d' person.txt|sed 's#101#100#g'
100 aaaaaa
102 BBBBB
方法二:推荐此类方法

[root@localhost ~]# sed -e '3,$d' -e 's#101#100#g' person.txt
100 aaaaaa
102 BBBBB
方法三:

[root@localhost ~]# sed '3,$d;s#101#100#g' person.txt
100 aaaaaa
102 BBBBB
方法四:先创建一个sed脚本,已sed -f执行sed脚本

[root@localhost ~]# cat person.sed
3,$d
s#101#100#g
[root@localhost ~]# sed -f person.sed person.txt
100 aaaaaa
102 BBBBB

企业案例:

一个文件100行,把5,35,70行单独拿出来

[root@localhost ~]# sed -n '5p;35p;70p' /etc/services
# IANA services version: last updated 2013-04-10
qotd            17/tcp          quote
whois++         63/udp          whoispp

{}大括号

[root@localhost ~]# sed -n '2,4{p;=}' person.txt
102 BBBBB
2
103 ccDD
3
104 dddds
4

l打印不可见字符\t和$

[root@localhost ~]# sed -n 'l' person.txt
101 aaaaaa$
102 BBBBB$
103 ccDD$
104 dddds$
105 wetsdg$

 

posted on 2020-02-20 22:10  卡乐咪运维  阅读(273)  评论(0)    收藏  举报