sed 用法总结

sed是一个非交互式的流编辑器。所谓非交互式,是指使用sed只能在命令行下输入编辑命令来编辑文本,然后在屏幕上查看输出;而所谓流编辑器,是指sed每次只从文件(或输入)读入一行,然后对该行进行指定的处理,并将结果输出到屏幕(除非取消了屏幕输出又没有显式地使用打印命令),接着读入下一行。整个文件像流水一样被逐行处理然后逐行输出。


sed不是在原输入上直接进行处理的,而是先将读入的行放到缓冲区中,对缓冲区里的内容进行处理,处理完毕后也不会写回原文件(除非用shell的输出重定向来保存结果),而是直接输出到屏幕上。sed运行过程中维护着两个缓冲区,一个是活动的“模式空间(pattern space)”,另一个是起辅助作用的“暂存缓冲区(holding space)”。一般情况下,每当运行sedsed首先把第一行装入模式空间,进行处理后输出到屏幕,然后将第二行装入模式空间替换掉模式空间里原来的内容,然后进行处理,以此类推。


所有示例以文件quote.txt 为例

内容如下:

[search@dbl-vm-211-13 algorithm]$ cat quote.txt 
The honeysuckle band played all night long for only $90.
It was a evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.


1.显示与行相关的命令  sed  -n ‘****’ file

[search@dbl-vm-211-13 algorithm]$ sed -n '1p' quote.txt     //大约第一行
The honeysuckle band played all night long for only $90.
[search@dbl-vm-211-13 algorithm]$ sed -n '1,3p' quote.txt   //打印1-3行
The honeysuckle band played all night long for only $90.
It was a evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
[search@dbl-vm-211-13 algorithm]$ sed -n '/Neave/' quote.txt 
sed: -e expression #1, char 7: missing command
[search@dbl-vm-211-13 algorithm]$ sed -n '/Neave/'p quote.txt      //模式匹配,区分大小写
The local nurse Miss P.Neave was in attendance.
[search@dbl-vm-211-13 algorithm]$ sed -n 'The'p quote.txt 
sed: can't find label for jump to `hep'
[search@dbl-vm-211-13 algorithm]$ sed -n '/The/'p quote.txt     //匹配时要加双斜杠,用-n参数
The honeysuckle band played all night long for only $90.
The local nurse Miss P.Neave was in attendance.
[search@dbl-vm-211-13 algorithm]$ sed -n '4,/The/'p quote.txt   //对第几行采用匹配模式
The local nurse Miss P.Neave was in attendance.
[search@dbl-vm-211-13 algorithm]$ sed -n '/\$/'p quote.txt        //特殊符号用\ 反斜杠转义,打印最后一行
The honeysuckle band played all night long for only $90.
[search@dbl-vm-211-13 algorithm]$ sed -n '1,$p' quote.txt     //打印1-最后一行
The honeysuckle band played all night long for only $90.
It was a evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
[search@dbl-vm-211-13 algorithm]$ sed -n '/.*ing/'p quote.txt   //打印以‘ing’结尾,前面为任意字母,出现0次或多次
It was a evening of splendid music and company.
[search@dbl-vm-211-13 algorithm]$ sed -n '/*ing/'p quote.txt  
[search@dbl-vm-211-13 algorithm]$ sed -n '/.*ing/'p quote.txt 
It was a evening of splendid music and company.
[search@dbl-vm-211-13 algorithm]$ sed -n '1p' quote.txt 
The honeysuckle band played all night long for only $90.
[search@dbl-vm-211-13 algorithm]$ sed -n '$p' quote.txt      // 打印最后一行
The local nurse Miss P.Neave was in attendance.
[search@dbl-vm-211-13 algorithm]$ sed -e '/music/=' quote.txt     //打印模式匹配行号  采用-e参数
The honeysuckle band played all night long for only $90.
2
It was a evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
[search@dbl-vm-211-13 algorithm]$ sed -n '/music/=' quote.txt     //只打印行号 采用-n参数
2

采用脚本形式对文件内容进行增删改查

[search@dbl-vm-211-13 algorithm]$ cat mix.sed 

#!/bin/sed -f 
#1.this is the change on line 1  把第一行替换掉
1 c\
The Dibble band were grooving
#2.insert a line   采用模式匹配方式找到改行,然后在改行的前面一行插入指定内容
/evening/ i\
They played some great tunes.
#3.change the lase line ,$ mean the last line  替换(change)最后一行
$ c\
Nurse Neave was too tipsy to help
#4.append a new line before the last line     在第三行后面追加一行内容
3 a\
Where was the nurse to help?

删除指定行
不用-n参数    

[search@dbl-vm-211-13 algorithm]$ sed '1d' quote.txt 
It was a evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.
[search@dbl-vm-211-13 algorithm]$ sed '$d' quote.txt  
The honeysuckle band played all night long for only $90.
It was a evening of splendid music and company.
Too bad the disco floor fell through at 23:10.

查找替换

[search@dbl-vm-211-13 algorithm]$ sed 's/\$//' quote.txt   //找到$号,换成空白
The honeysuckle band played all night long for only 90.
It was a evening of splendid music and company.
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.   

 将找到的结果重定向到文件

sed 's/\$//w tihuan.out' quote.txt   

[search@dbl-vm-211-13 algorithm]$ cat tihuan.out 
The honeysuckle band played all night long for only 90.

[search@dbl-vm-211-13 algorithm]$ sed -n '/nurse/hello wang &/p' quote.txt        //找到后在同行插入
sed: -e expression #1, char 9: extra characters after command
[search@dbl-vm-211-13 algorithm]$ sed -n '/nurse/'p quote.txt 
The local nurse Miss P.Neave was in attendance.

2.读取文件插入操作

[search@dbl-vm-211-13 algorithm]$ sed  '/company./r score.txt' quote.txt   //在模式匹配行/company/ 后放置文本。
The honeysuckle band played all night long for only $90.
It was a evening of splendid music and company.
Marry   2143 78 84 77
Jack    2321 66 78 45
Tom     2122 48 77 71
Mike    2537 87 97 95
Bob     2415 40 57 62
Too bad the disco floor fell through at 23:10.
The local nurse Miss P.Neave was in attendance.

2.匹配后退出

[search@dbl-vm-211-13 algorithm]$ sed '/.p.*/q' quote.txt 
The honeysuckle band played all night long for only $90.

3.处理控制字符     sed  's/***/XXX/g'  file

[search@dbl-vm-211-13 algorithm]$ cat dos.txt 
12332##DISO##45.12^M
00332##LPSO##23.11^M
01299##USPD##34.46^M

[search@dbl-vm-211-13 algorithm]$ cat dos.txt |sed 's/##*/ /g' |sed 's/^0*//g'                  //用空格替换所有的#号,删除起始域中最前面的0(00)
12332 DISO 45.12^M
332 LPSO 23.11^M
1299 USPD 34.46^M


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值