sed是一个流编辑器(sed是stream editor的缩写),它可以对从标准输入流中得到的数据进行处理,然后把处理以后得到的结果输出到标准输出,而标准输出重定向到文件,这样处理后的结果就会保存在磁盘文件中。要处理的数据也可以来自其它进程的数据输出。
sed命令调用形式:
1.从管道中读取数据 $ some_command | sed 'edit commands'
2.使用命令行参数读取文件内容 $sed 'edit commands' files
sed命令的语法格式:
sed的脚本格式:sed [option] -f 'sed script'filename
sed的命令格式: sed [option] 'sed command'filename
sed命令的选项(option):
-n :只打印模式匹配的行
-e :直接在命令行模式上进行sed动作编辑,此为默认选项
-f :将sed的动作写在一个文件内,用–f filename 执行filename内的sed动作
-r :支持扩展表达式
-i :直接修改文件内容
[root@www localhost sed]# cat mytext
hello worlf
hello linux
how are you
I am fine
#进行替换将worlf改为world
[root@www localhost sed]# sed 's/worlf/world/' mytext
hello world
hello linux
how are you
I am fine
[root@www localhost sed]# sed -i 's/worlf/world/' mytext
#加上-i文件内容直接改变
#-n只显示匹配到的函数
[root@www localhost sed]# sed -n '/world/p' mytext
hello world
[root@www localhost sed]# sed '/world/p' mytext
hello world
hello world
hello linux
how are you
I am fine
#前面的数字可以控制匹配的数字
[root@www localhost sed]# sed -n '/hello/p' mytext
hello world
hello linux
[root@www localhost sed]# sed -n '/hello/{1p}' mytext
hello world
#=输出行数
[root@www localhost sed]# sed -n '/hello/{=;p}' mytext
1
hello world
2
hello linux
#!取反,
[root@www localhost sed]# sed -n '/hello/!{=;p}' mytext
3
how are you
4
I am fine
#将how改为what并写入1.txt中
[root@www localhost sed]# sed -n 's/how/what/w 1.txt' mytext
[root@www localhost sed]# cat 1.txt
what are you
#匹配到fine是将1.txt的文件写入到mytext
[root@www localhost sed]# sed -i '/fine/r 1.txt' mytext
[root@www localhost sed]# cat mytext
hello worlf
hello linux
how are you
I am fine
what are you
####使用正则表达式、扩展正则表达式
^匹配行的开始
$匹配行的结尾
^$空白行
.匹配任意单个字符
*匹配紧挨在前面的字符任意次(0,1,多次)
.*匹配任意长度的任意字符
\?匹配紧挨在前面的字符0次或1次
\{m,n\}匹配其前面的字符至少m次,至多n次
[]匹配指定范围内的任意单个字符
[^]匹配指定范围外的任意单个字符
[:digit:]所有数字, 相当于0-9, [0-9]---> [[:digit:]]
[:lower:]所有的小写字母
[:upper:]所有的大写字母
[:alpha:]所有的字母
[:alnum:]相当于0-9a-zA-Z
[:space:] 空白字符
[root@www localhost sed]# cat /etc/sysctl.conf
# System default settings live in /usr/lib/sysctl.d/00-system.conf.
# To override those settings, enter new settings here, or in an /etc/sysctl.d/<name>.conf file
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
[root@www localhost sed]# cat /etc/sysctl.conf |sed -n '/^#.*/p'
# System default settings live in /usr/lib/sysctl.d/00-system.conf.
# To override those settings, enter new settings here, or in an /etc/sysctl.d/<name>.conf file
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
[root@www localhost sed]# cat /etc/sysctl.conf |sed -n '/^#[^/*]/p'
# System default settings live in /usr/lib/sysctl.d/00-system.conf.
# To override those settings, enter new settings here, or in an /etc/sysctl.d/<name>.conf file
# For more information, see sysctl.conf(5) and sysctl.d(5).
#^表示开头 $表示结尾 &表示正则匹配到的结果
[root@www localhost sed]# sed '/worlf/s/Z/^/' mytext
hello worlf
hello linux
how are you
I am fine
what are you
[root@www localhost sed]# sed '/worlf/s/$/XC/' mytext
hello worlfXC
hello linux
how are you
I am fine
what are you
[root@www localhost sed]# sed 's/worlf/zxc &/' mytext
hello zxc worlf
hello linux
how are you
I am fine
what are you
#-e连接两个语句
[root@www localhost sed]# sed -e 's/how/what/' -e 's/I/You/' mytext
hello worlf
hello linux
what are you
You am fine
what are you
#也可以用;链接
[root@www localhost sed]# sed 's/how/what/;s/I/You/' mytext
hello worlf
hello linux
what are you
You am fine
what are you
#html.txt
http://www.baidu.com/index.<a target="_blank"
http://www.2cto.com/kf
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
#用\来进行转义
[root@www localhost sed]# sed 's/http:\/\///;s/\/.*//' html.txt |sort |uniq -c|sort -rn
3 www.baidu.com
2 post.baidu.com
1 www.2cto.com
1 mp3.baidu.com
( 写于2016年5月20日,http://blog.csdn.net/bzd_111)