Linux的sed命令

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值