sed 命令详解

平凡
2018-07-06 14:47:30
332 阅读

sed(Stream Editor)是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。

sed命令的语法格式:

sed的命令格式: sed [option] 'sed command' filename
sed的脚本格式:sed [option] -f 'sed script' filename

sed命令的选项(option):

-n :只打印模式匹配的行
-e :直接在命令行模式上进行sed动作编辑,此为默认选项
-f :将sed的动作写在一个文件内,用–f filename 执行filename内的sed动作
-r :支持扩展表达式
-i :直接修改文件内容

sed在文件中查询文本的方式:

  1. 使用行号,可以是一个简单数字,或是一个行号范围
语法备注
xx为行号
x,y表示行号从x到y
/pattern查询包含模式的行
/pattern /pattern查询包含两个模式的行
pattern/,x在给定行号上查询包含模式的行
x,/pattern/通过行号和模式查询匹配的行
x,y!查询不包含指定行号x和y的行
# 文件内容
$ cat >> file1 << EOF     
heredoc> This is the header line
heredoc> This is the first data line
heredoc> This is the second data line
heredoc> This is the last line
heredoc> EOF

# 打印文件中的第2行,第2行会打印两遍,sed默认会打印文件中的所有行。
$ sed '2p' file1
This is the header line
This is the first data line
This is the first data line
This is the second data line
This is the last line

# -n选项表示只打印匹配的行
$ sed -n '2p' file1
This is the first data line

# 打印第1行到第3行的数据
$ sed -n '1,3p' file1
This is the header line
This is the first data line
This is the second data line

# 打印文件中匹配second字符的行
$ sed -n '/second/p' file1
This is the second data line

# 打印文件中从匹配first的行到第4行,first所处的行如果在第4行之后,则表示仅打印匹配到的first的行
$ sed -n '/first/,4p' file1
This is the first data line
This is the second data line
This is the last line

# 打印从第2行开始匹配到第一次出现last字符的行,last字符行也打印
$ sed -n '2,/last/p' file1
This is the first data line
This is the second data line
This is the last line

# 打印从匹配到first字符的行到首次匹配到last字符的行
$ sed -n '/first/,/last/p' file1
This is the first data line
This is the second data line
This is the last line

# 打印文件中第1行到第4行的内容,且打印行号,当用到sed不同的编辑命令时,用{},且不同编辑命令之间用分号
$ sed -n '1,4{=;p}' file1
1
This is the header line
2
This is the first data line
3
This is the second data line
4
This is the last line

# 用!表示对前面的匹配的模式取反
$ sed -n '1,2!{=;p}' file1
3
This is the second data line
4
This is the last line

$ sed -n '1,2!p' file1
This is the second data line
This is the last line
  1. 使用正则表达式、扩展正则表达式(必须结合-r选项)
元字符说明
^匹配行开始,如:/^sed/匹配所有以sed开头的行
$匹配行结束,如:/sed$/匹配所有以sed结尾的行
^$空白行
.匹配任意单个字符
*匹配0个或多个紧挨在前面的字符
.*匹配任意长度的任意字符
\?匹配紧挨在前面的字符0次或1次
\{m,n\}匹配其前面的字符至少m次,至多n次
\{m,\}匹配其前面的字符至少m次
\{m\}精确匹配前面的m次
\<匹配单词的开始----相当于 \b,用法格式:\<pattern
\>匹配单词的结束,用法格式:\>pattern
\<pattern\>匹配单词
[]匹配指定范围内的任意单个字符
[^]匹配指定范围外的任意单个字符
[:digit:]匹配所有数字, 相当于0-9, [0-9]---> [:digit:]
[:lower:]匹配所有的小写字母
[:upper:]匹配所有的大写字母
[:alpha:]匹配所有的字母
[:alnum:]相当于0-9a-zA-Z
[:space:]空白字符
[:punct:]匹配所有标点符号
  1. sed的编辑命令(sed command):
命令说明
p打印匹配行(和-n选项一起合用)
=显示文件行号
a\在定位行号后附加新文本信息
i\在定位行号后插入新文本信息
d删除定位行
c\用新文本替换定位文本
w filename写文本到一个文件,类似输出重定向 >
r filename从另一个文件中读文本,类似输入重定向 <
s使用替换模式替换相应模式
q第一个模式匹配完成后退出或立即退出
l显示与八进制ACSII代码等价的控制符
{}在定位行执行的命令组,用分号隔开
n从另一个文件中读文本下一行,并从下一条命令而不是第一条命令开始对其的处理
N在数据流中添加下一行以创建用于处理的多行组
g将模式2粘贴到/pattern n/
y传送字符,替换单个字符
  1. 定界符

以上命令中字符 / 在sed中作为定界符使用,也可以使用任意的定界符:

$ sed 's:test:TEXT:g'
$ sed 's|test|TEXT|g'

定界符出现在样式内部时,需要进行转义:

$ sed 's/\/bin/\/usr\/local\/bin/g'

对文件的操作无非就是”增删改查“,怎样用sed命令实现对文件的”增删改查“,玩转sed是写自动化脚本必须的基础之一。

sed命令实现对文件内容的添加:(对源文件添加的话就用-i参数):

# sed命令可以实现的添加
# 1)匹配行的行首添加,添加在同行  
# 2)匹配行的行中的某个字符后添加  
# 3)匹配行的行尾添加字符  
# 4)匹配行的行前面行添加  
# 5)匹配行的行后面行添加  
# 6)文件的行首添加一行  
$ sed -i '1 i\sed command start' file1
#7)文件的行尾追加一行  
$ sed -i '$a \sed command end' file1
# 文件内容
$ cat >> file2 << EOF
heredoc> hello world
heredoc> hello linux
heredoc> how are you
heredoc> thanks, and you
heredoc> EOF

# 在匹配world字符的行首添加 'Li '字符
$ sed '/world/s/^/Li /' file2
Li hello world
hello linux
how are you
thanks, and you

# 在匹配linux字符前面添加 'jie '字符
$ sed 's/linux/jie &/' file2
hello world
hello jie linux
how are you
thanks, and you

# 在匹配are字符的行的前面添加一行 nihao
$ sed '/are/i\nihao' file2
hello world
hello linux
nihao
how are you
thanks, and you

# 在匹配are字符的行的后面添加一行 nihao
$ sed '/are/a\nihao' file2
hello world
hello linux
how are you
nihao
thanks, and you

重点:sed命令实现对文件内容的替换(替换是在shell自动化脚本中用到最多的操作)

# 文件内容
$ cat >> file3 << EOF
heredoc> anonymous_enable=YES
heredoc> write_enable=YES
heredoc> local_umask=022
heredoc> xferlog_enable=YES
heredoc> connect_from_port_20=YES
heredoc> root:x:0:0:root:/root:/bin/bash
heredoc> bin:x:1:1:bin:/bin:/sbin/nologin
heredoc> daemon:x:2:2:daemon:/sbin:/sbin/nologin
heredoc> adm:x:3:4:adm:/var/adm:/sbin/nologin
heredoc> lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
heredoc> DEVICE="eth0"
heredoc> BOOTPROTO="static"
heredoc> HWADDR="00:0C:29:90:79:78"
heredoc> ONBOOT="yes"
heredoc> IPADDR=172.16.22.1
heredoc> NETMASK=255.255.0.0
heredoc> EOF

# 匹配DEVICE的行,替换成Ethernet这行
$ sed -i '/DEVICE/c\Ethernet' file3

 # 把static替换成dhcp
$ sed -i 's/static/dhcp/' file3

# 匹配IPADDR的行,把22.1替换成10.12由于.号有特殊意义所有需要转义  
$ sed -i '/IPADDR/s@22\.1@10.12@' file3

# 匹配connect的行,把YES替换成NO
$ sed -i '/connect/s#YES#NO#' file3
 
# 把所有匹配到bin的行中第二次及第二次之后出现bin替换成tom  
$ sed -i 's/bin/tom/2g' file3

# 把所有匹配到bin的行中第二次出现的daemon替换成jerry,并在生产与匹配行同样的行  
$ sed -i 's/daemon/jerry/2p' file3

# 把所有匹配到adm的行中仅仅只是第二次出现的adm替换成boss  
$ sed -i 's/adm/boss/2' file3

# 匹配root的行,把bash替换成nologin,且把0替换成1  
$ sed -i '/root/{s/bash/nologin/;s/0/1/g}' file3

# 把root用括号括起来,&表示引用前面匹配的字符
$ sed -i 's/root/(&)/g' file3

# 匹配BOOTPROTO替换成#BOOTPROTO,在配置文件中一般用于注释某行
$ sed -i 's/BOOTPROTO/#BOOTPROTO/' file3

# 匹配ONBOOT的行的前面添加#号,在配置文件中也表示注释某行
$ sed -i 's/ONBOOT/#&/' file3

# 匹配ONBOOT的行,把#替换成空,即去掉#号,也一般用作去掉#注释
$ sed -i '/ONBOOT/s/#//' file3

# 执行以上sed命令之后文件显示的内容
$ cat file3 
anonymous_enable=YES
write_enable=YES
local_umask=022
xferlog_enable=YES
connect_from_port_20=NO
(root):x:1:1:(root):/(root):/bin/nologin
bin:x:1:1:tom:/tom:/stom/nologin
daemon:x:2:2:jerry:/sbin:/stom/nologin
daemon:x:2:2:jerry:/sbin:/stom/nologin
adm:x:3:4:boss:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Ethernet
#BOOTPROTO="dhcp"
HWADDR="00:0C:29:90:79:78"
ONBOOT="yes"
IPADDR=172.16.10.12
NETMASK=255.255.0.0

sed引用变量:(在自动化shell脚本 中也经常会使用到变量)
第一种当sed命令里面没有默认的变量时可以把单引号改成双引号;
第二种当sed命令里面有默认的变量时,那自己定义的变量需要加单引号,且sed里面的语句必须用单引

# 文件内容
$ cat >> file4 << EOF
heredoc> hello world
heredoc> i am jie
heredoc> how are you
heredoc> EOF

# 定义一个变量,且给变量赋值
$ name=li

# 把匹配jie的字符替换成变量的值
$ sed -i "s/jie/$name/" file4

$ cat file4 
hello world
i am li
how are you

# 当sed命令也有默认变量时,在去引用自己定义的变量会出现语法错误
$ sed -i "$a $name" file4 
sed: -e expression #1, char 3: extra characters after command

# 在引用自定义的变量时,sed语句必须用单引引住,然后把自定义的变量也用单引号引住
$ sed -i '$a '$name'' file4
  
$ cat file4 
hello world
i am li
how are you
li

sed的其它高级使用:

1)把正在用sed操作的文件的内容写到例外一个文件中

# sed操作的文件中的内容
$ cat >> file5 << EOF
heredoc> Ethernet
heredoc> #BOOTPROTO="dhcp"
heredoc> HWADDR="00:0C:29:90:79:78"
heredoc> ONBOOT="yes"
heredoc> IPADDR=172.16.10.12
heredoc> NETMASK=255.255.0.0
heredoc> EOF

# 把sed操作的文件内容保存到另外一个文件中,w表示保存,ip.txt文件名
$ sed -i 's/IPADDR/ip/w ip.txt' file5

# 查看新文件的内容  
$ cat ip.txt
ip=172.16.10.12

$ cat file5 
Ethernet
#BOOTPROTO="dhcp"
HWADDR="00:0C:29:90:79:78"
ONBOOT="yes"
ip=172.16.10.12
NETMASK=255.255.0.0

2)读取一个文件到正在用sed操作的文件中

# 文件内容
$ cat >> file6 << EOF
heredoc> hello world
heredoc> i am li
heredoc> how are you
heredoc> li
heredoc> EOF

$ cat file5 
Ethernet
#BOOTPROTO="dhcp"
HWADDR="00:0C:29:90:79:78"
ONBOOT="yes"
ip=172.16.10.12
NETMASK=255.255.0.0

# 在匹配are的行,读进来另一个文件的内容,读进来的文件的内容会插入到匹配are的行后
$ sed  -i '/are/r file5' file6

# 再次查看用sed命令操作的行
$ cat file6 
hello world
i am li
how are you
Ethernet
#BOOTPROTO="dhcp"
HWADDR="00:0C:29:90:79:78"
ONBOOT="yes"
ip=172.16.10.12
NETMASK=255.255.0.0
li

sed的经典例子:

#1)、处理以下文件内容,将域名取出并进行计数排序,如处理:  
http://www.baidu.com/index.
http://www.baidu.com/1.html
http://post.baidu.com/index.html  
http://mp3.baidu.com/index.html
http://www.baidu.com/3.html
http://post.baidu.com/2.html
得到如下结果:  
域名的出现的次数 域名
3 www.baidu.com  
2 post.baidu.com  
1 mp3.baidu.com

# 生成文件内容
$ cat >> file7 << EOF
heredoc> http://www.baidu.com/index.
heredoc> http://www.baidu.com/1.html
heredoc> http://post.baidu.com/index.html  
heredoc> http://mp3.baidu.com/index.html
heredoc> http://www.baidu.com/3.html
heredoc> http://post.baidu.com/2.html
heredoc> EOF

$ cat file7 | sed -e ' s/http:\/\///' -e ' s/\/.*//' | sort | uniq -c | sort -rn
3 www.baidu.com  
2 post.baidu.com  
1 mp3.baidu.com

$ awk -F/ '{print $3}' file7 | sort -r | uniq -c | awk '{print $1"\t",$2}'
3	 www.baidu.com
2	 post.baidu.com
1	 mp3.baidu.com

学会sed的使用是写自动化shell脚本的基础,sed也是一个非常有用且重要的命令,是文本处理工具之一,以上是我自己学习总结的sed命令简单的用法,sed还有更高级的用法,也还在学习中。

0 评论
暂无数据