您的位置:首页 > 其它

Sed练习笔记

2012-03-21 15:26 113 查看
笔记内容:
*抽取域 
*匹配正则表达式 
*比较域 
*增加、附加、替换 
sed [选项] sed命令 输入文件 (命令要加单引号) 
sed [选项] -f sed脚本文件 输入文件    (脚本解释器) 

***【参数】*** 
a、文本定位参数 
x 行号    x,y 行号范围     
/pattern/ 查询包含pattern的行 /pattern/part/ 查询包含两个关键字的行 
pattern/,x 在给定行号上查询pattern 
x,/pattern 通过行号和关键字查询匹配行 
x,y! 查询不包含行号为x和y的行 

b、sed编辑命令 
p 打印匹配行 = 显示文件行号     
a\ 在定位行号后附加信文本信息    i\ 在定位行号后插入新文本信息 
d 删除定位行 c\ 用新文本替换定位文件 
s 使用替换模式替换相应关键字 r 从另一个文件中读文本 
w 写文本到一个文件  y 传送字符  n 安静模式,只显示修改的 

***【编程举例】*** 
# cat file 
The honeysuckle band played all night long for only $90. 
It was an 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、p(rint) 显示第二行 
# sed '2p' file 
The honeysuckle band played all night long for only $90. 
It was an evening of splendid music and company. 
It was an 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 -n '2p' file 
It was an evening of splendid music and company. 

2、打印范围 1-3 
# sed -n '1,3p' file 
The honeysuckle band played all night long for only $90. 
It was an evening of splendid music and company. 
Too bad the disco floor fell through at 23:10. 

3、打印关键字 Neave 
# sed -n '/Neave/p' file 
The local nurse Miss P.Neave was in attendance. 

4、使用关键字和行号进行查询 The 
# sed -n '/The/p' file 
The honeysuckle band played all night long for only $90. 
The local nurse Miss P.Neave was in attendance. 

# sed -n '4,/The/p' file  
The local nurse Miss P.Neave was in attendance. 

5、匹配元字符$    (必须使用\屏蔽特殊含义) 
# sed -n '/\$/p' file 
The honeysuckle band played all night long for only $90. 

6、显示整个文件 
# sed -n '1,$p' file 
The honeysuckle band played all night long for only $90. 
It was an 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. 

7、任意以ing结尾的  后跟任意字母0次或多次,以ing结尾,模式为/.*ing/ 
# sed -n '/.*ing/p' file  
It was an evening of splendid music and company. 

8、打印行首、打印最后一行 
# sed -n '1p' file  
The honeysuckle band played all night long for only $90. 
# sed -n '$p' file  
The local nurse Miss P.Neave was in attendance. 

9、打印行号  (使用=号,格式为/pattern/=) 
# sed -n '/music/=' file  
2 
# sed -e '/music/=' file  
The honeysuckle band played all night long for only $90. 
2 
It was an 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 -n -e '/music/p' -e '/music/=' file 
It was an evening of splendid music and company. 
2 

10、附加文本,使用 a\ ,可以将指定文本一行或多行附加到指定行。’\’表示换行 
举例:创建脚本append.sed 
# vim append.sed 
#!/bin/sed -f  
/company/ a\ 
Then suddenly it happened. 
# chmod +x append.sed 
# ./append.sed file  
The honeysuckle band played all night long for only $90. 
It was an evening of splendid music and company. 
Then suddenly it happened. 
Too bad the disco floor fell through at 23:10. 
The local nurse Miss P.Neave was in attendance. 

11、插入文本,类似于附加文本,只是它在行前面插入 
举例:创建脚本insert.sed 
# vim insert.sed 
#!/bin/sed -f  
/attendance/ i\ 
Utter confusion followed. 
# chmod +x insert.sed 
#./insert.sed file  
The honeysuckle band played all night long for only $90. 
It was an evening of splendid music and company. 
Too bad the disco floor fell through at 23:10. 
Utter confusion followed. 
The local nurse Miss P.Neave was in attendance. 

12、修改文本,修改命令将用新文本替代关键字所在行的文本 
举例:创建脚本change.sed 
# vim change.sed 
#!/bin/sed -f 
/honeysuckle/ c\ 
The Office Dibble band played well. 
# chmod +x change.sed 
#./change.sed file  
The Office Dibble band played well. 
It was an 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. 

也可以使用行号来替换: 
#!/bin/sed -f 
3 c\ 
The Office Dibble band played well. 

13、混合附加、插入、修改文本的脚本例子 
# vim mix.sed 
#!/bin/sed -f 
#This is the change on line 1 
1 c\ 
The Dibble band were grooving. 
#Then insert a line 
/evening/ i\ 
They played some great tunes. 
#change the last line, a $ means lasr line 
$ c\ 
Nurse Neave was too tipsy to help 
#stick in a new line before the last line 
3 a\ 
Where was the nurse to help? 
# chmod +x mix.sed  
# ./mix.sed file  
The Dibble band were grooving. 
They played some great tunes. 
It was an evening of splendid music and company. 
Too bad the disco floor fell through at 23:10. 
Where was the nurse to help? 
Nurse Neave was too tipsy to help 

14、删除文本,可以是行号,行范围,最后一行,正则表达式匹配删除等 
# sed '1d' file  
It was an 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 '1,3d' file  
The local nurse Miss P.Neave was in attendance. 
# sed '$d' file  
The honeysuckle band played all night long for only $90. 
It was an evening of splendid music and company. 
Too bad the disco floor fell through at 23:10. 
# sed '/Neave/d' file  
The honeysuckle band played all night long for only $90. 
It was an evening of splendid music and company. 
Too bad the disco floor fell through at 23:10. 

15、替换文本,格式为[a,b]s/pre/post/[g p w n] 
s 是一个替换操作,查询pre,替换为post 
g 默认情况下只替换第一次匹配到的关键字,g为全局替换 
p 默认sed将所有被替换的行都显示出来,加p选项则-n无效。 
w 文件名,此选项将发生改变的行写入到另一个文件中 
n 不打印输出结果 
举例: 
# sed 's/night/NIGHT/' file  
The honeysuckle band played all NIGHT long for only $90. 
It was an 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/The/Wow!/g' file  
Wow! honeysuckle band played all night long for only $90. 
It was an evening of splendid music and company. 
Too bad the disco floor fell through at 23:10. 
Wow! local nurse Miss P.Neave was in attendance. 
# sed 's/splendid/SPLENDID/w sed.out' file (文件名在引号里边) 
The honeysuckle band played all night long for only $90. 
It was an 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. 
# cat sed.out  
It was an evening of SPLENDID music and company. 

***【使用替换修改字符串】*** 
要附加或修改一个字符传,可以使用&命令。&在查找出来的关键字前面加上自己的字符串 
格式为: s/nurse/"Hello"&/p 
举例: 
# sed -n 's/nurse/"Hello" &/p' file  
The local "Hello" nurse Miss P.Neave was in attendance. 

***【将sed结果写入另一个文件】*** 
格式: [2,n] w filename 
举例: 
# sed '1,2 w filedt' file (将1,2行输出到文件filedt中) 
The honeysuckle band played all night long for only $90. 
It was an 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. 
# cat filedt  
The honeysuckle band played all night long for only $90. 
It was an evening of splendid music and company. 
# sed '/Neave/ w dht' file (将关键字Neave所在行写入到文件dht中) 
The honeysuckle band played all night long for only $90. 
It was an 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. 
# cat dht  
The local nurse Miss P.Neave was in attendance. 

***【从文件中读文本】*** 
sed允许从另一个文件中读文本,并将其文本附加在当前文件,此命令放在匹配行后面 
举例: 
# echo "Boom boom went to the music." > sedex.txt 
# sed '/company/r sedex.txt' file (将文件sedex.txt的内容加到company行后) 
The honeysuckle band played all night long for only $90. 
It was an evening of splendid music and company. 
Boom boom went to the music. 
Too bad the disco floor fell through at 23:10. 
The local nurse Miss P.Neave was in attendance. 

***【匹配后退出】*** 
举例:查询/.a.*/, 
# sed '/a/q' file (在第一行就查到了a关键字,退出) 
The honeysuckle band played all night long for only $90. 
# sed '/m/q' file (在第二行查询到m关键字,退出) 
The honeysuckle band played all night long for only $90. 
It was an evening of splendid music and company. 

***【处理字符实例--处理控制字符】*** 
# vim dos.txt 
12332##DISO##45.12^M 
00332##LPSO##23.11^M 
01299###USPD###34.46^M 
a、用一个空格替换掉## 
b、删除起始域中最前面的0 
c、删除行尾控制字符 
# sed 's/##*/ /g' dos.txt (替换##为空格) 
12332 DISO 45.12^M 
00332 LPSO 23.11^M 
01299 USPD 34.46^M 
# sed 's/^0*//g' dos.txt  (将0替换掉) 
12332##DISO##45.12^M 
332##LPSO##23.11^M 
1299###USPD###34.46^M 
# sed 's/\^M//g' dos.txt  (将^M替换掉) 
12332##DISO##45.12 
00332##LPSO##23.11 
01299###USPD###34.46 

***【处理字符实例--处理报文输出】*** 
# vim sql.txt 
Database        Size(MB)        Date Created 
-------------------------------------------- 
GOSOUTH         2244            12/11/97 
TRISUD          5632            8/9/99 
(2 rows affected) 

处理数据使得到下面的数据: 
GOSOUTH 
TRISUD 

解决思路: 
a、使用s/-*//g 删除横线------ 
b、使用/^$/d 删除空行 
c、使用$d 删除最后一行 
d、使用1d删除第一行 
e、使用awk{print $1}打印第一行 

# cat sql.txt | sed 's/-*//g' | sed '/^$/d' | sed '1d' | sed '$d' | awk '{print $1}' 
GOSOUTH 
TRISUD 

***【处理字符实例--去除行首数字】*** 
# vim UNH.txt 
12345UND  SPLLFC  234344 
9999999UND  SKKLT  3423 
1UND  SPLLY  434 

# sed 's/^[0-9]*//g' UNH.txt  
UND  SPLLFC  234344 
UND  SKKLT  3423 
UND  SPLLY  434 

***【处理字符实例--附加文本Passed】*** 
# vim ok.txt 
AC456   
AC492169 
AC9967 
AC88345 
# sed 's/[0-9][0-9]*/& Passed/g' ok.txt (&与Passed的位置关系决定了Passed的放在前还是后,这里是放在后面) 
AC456 Passed     
AC492169 Passed  
AC9967 Passed    
AC88345 Passed   

***【处理字符实例--从shell向sed传值及从sed输出中设置shell变量】*** 
# name="It's a go situation" 
# replace=nice 
# echo $name |sed "s/go/$replace/g" (注意双引号) 
It's a nice situation 

# new_name=`echo $name |sed "s/go/$replace/g"` 
# echo $new_name 
It's a nice situation 

***【快速一行命令集】*** 
's/\.$//g'  删除句尾的点 
'-e /abcd/d'    删除包含abcd的行 
's/  */ /g'     删除一个以上的空格,用一个空格代替 
's/^  *//g' 删除行首空格 
's/\.  */ /g'   删除句点后跟两个或更多的空格,用一个空格代替 
's/^$/d'    删除空行 
's/^.//g'   删除行首字符 
's/COL\(...\)//g'   删除紧跟COL后的三个字母 
's/^\///g'  删除全文的/ 
's/^\///'   删除全文每一行出现的第一个/ 
's/ / //g' 
实例: 
1、删除路径名第一个/符号 
# echo $PWD | sed 's/^\///'  
root/Desktop 

2、追加/插入文本 
# echo "Mr Willis" | sed 's/Mr/& Bruce/g' 
Mr Bruce Willis 

3、删除首字符 
# echo "accounts.doc" |sed 's/^.//' 
ccounts.doc 

4、删除文件扩展名 
# echo "accounts.doc" |sed 's/\..*//g' 
accounts 

5、增加文件扩展名 
# echo accounts | sed 's/$/&.doc/' 
accounts.doc 

6、替换字符系列(+变为of,%换为located) 
# x="Department+payroll%Building G" 
# echo $x |sed 's/+/ of /g' |sed 's/%/ located /g' 
Department of payroll located Building G 
(RHEL6成功  +和%不算是特殊字符吗?) 

2012-3-21练习笔记
本文出自 “梦想年华 @楚国小生” 博客,请务必保留此出处http://linuxkeep.blog.51cto.com/1010009/812712
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: