sed:Stream Editor文本流编辑,sed是一个“非交互式的”面向字符流的编辑器。能同时处理多个文件多行的内容,可以不对原文件改动,把整个文件输入到屏幕,可以把只匹配到模式的内容输入到屏幕上。还可以对原文件改动,但是不会再屏幕上返回结果。
sed命令的语法格式:
sed的命令格式: sed [option] ‘sed command’filename
sed命令的选项(option):
-n :只打印模式匹配的行
-e :直接在命令行模式上进行sed动作编辑,此为默认选项
-f :将sed的动作写在一个文件内,用–f filename 执行filename内的sed动作
-r :支持扩展表达式;脱意
-i :直接修改文件内容
2)使用正则表达式、扩展正则表达式(必须结合-r选项)
^ | 锚点行首的符合条件的内容,用法格式”^pattern” |
---|---|
$ | 锚点行首的符合条件的内容,用法格式”pattern$” |
^$ | 空白行 |
. | 匹配任意单个字符 |
* | 匹配紧挨在前面的字符任意次(0,1,多次) |
.* | 匹配任意长度的任意字符 |
\? | 匹配紧挨在前面的字符0次或1次 |
\{m,n\} | 匹配其前面的字符至少m次,至多n次 |
\{m,\} | 匹配其前面的字符至少m次 |
\{m\} | 精确匹配前面的m次\{0,n\}:0到n次 |
\< | 锚点词首—-相当于 \b,用法格式:\ |
\> | 锚点词尾,用法格式:\>pattern |
\ | 单词锚点 |
\(\) | 分组,用法格式:\(pattern\),引用\1,\2 |
[] | 匹配指定范围内的任意单个字符 |
[^] | 匹配指定范围外的任意单个字符 |
[:digit:] | 所有数字, 相当于0-9, [0-9]—> [[:digit:]] |
[:lower:] | 所有的小写字母 |
[:upper:] | 所有的大写字母 |
[:alpha:] | 所有的字母 |
[:alnum:] | 相当于0-9a-zA-Z |
[:space:] | 空白字符 |
[:punct:] | 所有标点符号 |
#sed '5 p' /etc/passwd#打印前5行
#sed -n '/r*t/p' /etc/passwd #打印匹配r有0个或者多个,后接一个t字符的行
#sed -n '/.r.*/p' /etc/passwd #打印匹配有r的行并且r后面跟任意字符
#sed -n '/o*/p' /etc/passwd #打印o字符重复任意次
#sed -n '/o\{1,\}/p' /etc/passwd #打印o字重复出现一次以上
#sed -n '/o\{1,3\}/p' /etc/passwd #打印o字重复出现一次到三次之间以上
对文件的操作无非就是”增删改查“,怎样用sed命令实现对文件的”增删改查“,玩转sed是写自动化脚本必须的基础之一。
#sed -n '/^#/!p' /etc/vsftpd/vsftpd.conf
#sed -n '/^#/!{/^$/!p}' /etc/vsftpd/vsftpd.conf
#sed -e '/^#/d' -e '/^$/d' /etc/vsftpd/vsftpd.conf
sed命令实现对文件内容的添加:(对源文件添加的话就用-i参数):
#1)匹配行的行首添加,添加在同行
#2)匹配行的行中的某个字符后添加
#3)匹配行的行尾添加字符
#4)匹配行的行前面行添加
#5)匹配行的行后面行添加
#6)文件的行首添加一行
[[email protected] ~]# sed -i ‘1 i\sed command start’ myfile
#7)文件的行尾追加一行
[[email protected] ~]# sed -i ‘$a \sed command end’ myfile
sed的删除操作是针对文件的行,如果想删除行中的某个字符,那就用替换!
1)把正在用sed操作的文件的内容写到例外一个文件中
[[email protected] ~]# cat test
Ethernet
#BOOTPROTO=”dhcp”
HWADDR=”00:0C:29:90:79:78″
ONBOOT=”yes”
IPADDR=172.16.10.12
NETMASK=255.255.0.0
[[email protected] ~]# sed -i ‘s/IPADDR/ip/w ip.txt’ test
#把sed操作的文件内容保存到另外一个文件中,w表示保存,ip.txt文件名
[[email protected] ~]# cat ip.txt
ip=172.16.10.12
2)读取一个文件到正在用sed操作的文件中
[[email protected] ~]# cat myfile
hello world
i am li
how are you
li
[[email protected] ~]# cat test
Ethernet
#BOOTPROTO=”dhcp”
HWADDR=”00:0C:29:90:79:78″
ONBOOT=”yes”
IPADDR=172.16.10.12
NETMASK=255.255.0.0
[[email protected] ~]# sed -i ‘/Ethernet/r myfile’ test
#在匹配Ethernet的行,读进来另一个文件的内容,读进来的文件的内容会插入到匹配Ethernet的行后
[[email protected] ~]# cat test
Ethernet
hello world
i am li
how are you
li
#BOOTPROTO=”dhcp”
HWADDR=”00:0C:29:90:79:78″
ONBOOT=”yes”
IPADDR=172.16.10.12
NETMASK=255.255.0.0
##1)、处理以下文件内容,将域名取出并进行计数排序,如处理:
http://www.baidu.com/index.html
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
[[email protected] shell]# cat file | sed -e ‘ s/http:\/\///’ -e ‘ s/\/.*//’ | sort | uniq -c | sort -rn
3 www.baidu.com
2 post.baidu.com
1 mp3.baidu.com
也许上面的不是太容易理解,老七整理了一个比较容易理解的给大家参考一下:
[[email protected] sedtest]# cat baidu.txt | sed -r -e ‘s#http://##g’ -e ‘s#/.*##g’ | sort -n |uniq -c 1 mp3.baidu.com 2 post.baidu.com 3 www.baidu.com