2013年(16)
分类: LINUX
2013-04-17 18:02:29
原文地址:
sed 用法介绍
sed是一个非交互性文本流编辑器。它编辑文件或标准输入导出的文本拷贝。
" 抽取域。
" 匹配正则表达式。
" 比较域。
" 增加、附加、替换。
" 基本的sed命令和一行脚本。
可以在命令行输入sed命令,也可以在一个文件中写入命令,然后调用sed,这与a w k基本相同。使用sed需要记住的一个重要事实是,无论命令是什么, sed并不与初始化文件打交道,它操作的只是一个拷贝,然后所有的改动如果没有重定向到一个文件,将输出到屏幕。
因为sed是一个非交互性编辑器,必须通过行号或正则表达式指定要改变的文本行。
本文介绍sed用法和功能。本章大多编写的是一行命令和小脚本。这样做可以慢慢加深对sed用法的了解,取得宝贵的经验,以便最终自己编出大的复杂sed脚本。
和g r e p与a w k一样, sed是一种重要的文本过滤工具,或者使用一行命令或者使用管道与g r e p与a w k相结合。
1 sed怎样读取数据
sed从文件的一个文本行或从标准输入的几种格式中读取数据,将之拷贝到一个编辑缓冲区,然后读命令行或脚本的第一条命令,并使用这些命令查找模式或定位行号编辑它。重复此过程直到命令结束。
2 调用sed
调用sed有三种方式:在命令行键入命令;将sed命令插入脚本文件,然后调用sed;将sed命令插入脚本文件,并使sed脚本可执行。
使用sed命令行格式为:
1. sed [选项] sed命令输入文件。
复制代码
记住在命令行使用sed命令时,实际命令要加单引号。sed也允许加双引号。
使用sed脚本文件,格式为:
1. sed [选项] -f sed脚本文件输入文件
复制代码
要使用第一行具有sed命令解释器的sed脚本文件,其格式为:
1. sed脚本文件[选项] 输入文件
复制代码
不管是使用s h e l l命令行方式或脚本文件方式,如果没有指定输入文件, sed从标准输入中接受输入,一般是键盘或重定向结果。
sed选项如下:
n 不打印;sed不写编辑行到标准输出,缺省为打印所有行(编辑和未编辑)。p命令可以用来打印编辑行。
c 下一命令是编辑命令。使用多项编辑时加入此选项。如果只用到一条sed命令,此选项无用,但指定它也没有关系。
f 如果正在调用sed脚本文件,使用此选项。此选项通知sed一个脚本文件支持所有的sed命令,例如:sed -f myscript.sed input_file,这里m y s c r i p t . sed即为支持sed命令的文件。
2.1 保存sed输出
由于不接触初始化文件,如果想要保存改动内容,简单地将所有输出重定向到一个文件即可。下面的例子重定向sed命令的所有输出至文件‘ m y o u t f i l e’,当对结果很满意时使用这种方法。
1. $sed 'some-sed-commands' input-file > myoutfile
复制代码
2.2 使用sed在文件中查询文本的方式
sed浏览输入文件时,缺省从第一行开始,有两种方式定位文本:
1) 使用行号,可以是一个简单数字,或是一个行号范围。
2 ) 使用正则表达式
下面是使用sed定位文本的一些方式。
1. x x为一行号,如1
2. x , y 表示行号范围从x到y,如2,5表示从第2行到第5行
3. / p a t t e r n / 查询包含模式的行。例如/ d i s k /或/[a-z]/
4. / p a t t e r n / p a t t e r n / 查询包含两个模式的行。例如/ d i s k / d i s k s /
5. p a t t e r n / , x 在给定行号上查询包含模式的行。如/ r i b b o n / , 3
6. x , / p a t t e r n / 通过行号和模式查询匹配行。3 . / v d u /
7. x , y ! 查询不包含指定行号x和y的行。1 , 2 !
复制代码
2.3 基本sed编辑命令
1. sed编辑命令
2. p 打印匹配行
3. = 显示文件行号
4. a \ 在定位行号后附加新文本信息
5. i \ 在定位行号后插入新文本信息
6. d 删除定位行
7. c \ 用新文本替换定位文本
8. s 使用替换模式替换相应模式
9. r 从另一个文件中读文本
10. w 写文本到一个文件
11. q 第一个模式匹配完成后推出或立即推出
12. l 显示与八进制A S C I I代码等价的控制字符
13. { } 在定位行执行的命令组
14. n 从另一个文件中读文本下一行,并附加在下一行
15. g 将模式2粘贴到/pattern n/
16. y 传送字符
17. n 延续到下一输入行;允许跨行的模式匹配语句
复制代码
sed和正则表达式
sed识别任何基本正则表达式和模式及其行匹配规则。记住规则之一是:如果要定位一特殊字符,必须使用( \)屏蔽其特殊含义
shell基础十二篇
sed例子中使用下述文本文件q u o t e . t x t。 1. [sam@Linux_chenwy sam]$ cat quote.txt 2. The honeysuckle band played all night long for only $90. 3. It was an evening of splendid music and company. 4. Too bad the disco floor fell through at 23:00. 5. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@Linux_chenwy sam]$ sed -n '2p' quote.txt 2. It was an evening of splendid music and company. 显示2到5行
sed –n ‘2,5p’ quote.txt 1. [sam@Linux_chenwy sam]$ sed -n '1,3p' quote.txt 2. The honeysuckle band played all night long for only $90. 3. It was an evening of splendid music and company. 4. Too bad the disco floor fell through at 23:00. 复制代码
1. [sam@Linux_chenwy sam]$ sed -n '/The/p' quote.txt 2. The honeysuckle band played all night long for only $90. 3. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@Linux_chenwy sam]$ sed -n '/The/p' quote.txt 2. The honeysuckle band played all night long for only $90. 3. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@Linux_chenwy sam]$ sed -n '4,/The/p' quote.txt 2. The local nurse Miss P.Neave was in attendance. 复制代码
找到行之前的全部打打印出来。 1. [sam@Linux_chenwy sam]$ sed -n '/\$/p' quote.txt 2. The honeysuckle band played all night long for only $90. 匹配斜杠
sed -n '/\//p' smit.log 1. [sam@Linux_chenwy sam]$ sed -n '1,$p' quote.txt 2. The honeysuckle band played all night long for only $90. 3. It was an evening of splendid music and company. 4. Too bad the disco floor fell through at 23:00. 5. The local nurse Miss P.Neave was in attendance. 复制代码
可以使用这个模式查询以i n g结尾的任意单词。 1. [sam@Linux_chenwy sam]$ sed -n '/.*ing/p' quote.txt 2. It was an evening of splendid music and company. 复制代码
1. [sam@Linux_chenwy sam]$ sed -n '1p' quote.txt 2. The honeysuckle band played all night long for only $90. 复制代码
1. [sam@Linux_chenwy sam]$ sed -n '$p' quote.txt 2. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@Linux_chenwy sam]$ sed -e '/music/=' quote.txt 2. The honeysuckle band played all night long for only $90. 3. 2 4. It was an evening of splendid music and company. 5. Too bad the disco floor fell through at 23:00. 6. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@Linux_chenwy sam]$ sed -n '/music/=' quote.txt 2. 2 复制代码
1. [sam@Linux_chenwy sam]$ sed -n -e '/music/p' -e '/music/=' quote.txt 2. It was an evening of splendid music and company. 3. 2 复制代码 |
11 附加文本
要附加文本,使用符号a \,可以将指定文本一行或多行附加到指定行。如果不指定文本放置位置, s e d缺省放在每一行后面。附加文本时不能指定范围,只允许一个地址模式。文本附加操作时,结果输出在标准输出上。注意它不能被编辑,因为s e d执行时,首先将文件的一行文本拷贝至缓冲区,在这里s e d编辑命令执行所有操作(不是在初始文件上),因为文本直接输出到标准输出,s e d并无拷贝。
要想在附加操作后编辑文本,必须保存文件,然后运行另一个s
e d命令编辑它。这时文件的内容又被移至缓冲区。
附加操作格式如下:
1. [address]a\
2. text\
3. text\
4. ......
5. text
复制代码
地址指定一个模式或行号,定位新文本附加位置。a\
通知s e d对a \后的文本进行实际附加操作。观察格式,注意每一行后面有一斜划线,这个斜划线代表换行。s e d执行到这儿,将创建一新行,然后插入下一文本行。最后一行不加斜划线, s e d假定这是附加命令结尾。
当附加或插入文本或键入几个s
e d命令时,可以利用辅助的s
h e l l提示符以输入多行命令。当附加或插入文本或键入几个s e d命令时,可以利用辅助的s h e l l提示符以输入多行命令。
shell基础十二篇
创建sed脚本文件 1. [sam@chenwy sam]$ cat append.sed 2. #!/bin/sed -f 3. /company/ a\ 4. Then suddenly it happed. 复制代码
1. [sam@chenwy sam]chmod u+x append.sed 2. [sam@chenwy sam]$ ./append.sed quote.txt 3. The honeysuckle band played all night long for only $90. 4. It was an evening of splendid music and company. 5. Then suddenly it happed. 6. Too bad the disco floor fell through at 23:00. 7. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@chenwy sam]$ sed "/company/a\Then suddenly it happened." quote.txt 2. The honeysuckle band played all night long for only $90. 3. It was an evening of splendid music and company. 4. Then suddenly it happened. 5. Too bad the disco floor fell through at 23:00. 6. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@chenwy sam]$ sed "/company/i\Utter confusion followed." quote.txt 复制代码
1. [sam@chenwy sam]$ cat insert.sed 2. #!/bin/sed -f 3. 4 i\ 4. Utter confusion followed. 复制代码
1. [sam@chenwy sam]$ chmod u+x insert.sed 2. [sam@chenwy sam]$ ./insert.sed quote.txt 3. The honeysuckle band played all night long for only $90. 4. It was an evening of splendid music and company. 5. Too bad the disco floor fell through at 23:00. 6. Utter confusion followed. 7. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@chenwy sam]$ cat change.sed 2. #!/bin/sed -f 3. 3 c\ 4. The office Dibble band played well. 复制代码
1. [sam@chenwy sam]$ chmod u+x change.sed 2. [sam@chenwy sam]$ ./change.sed quote.txt 3. The honeysuckle band played all night long for only $90. 4. It was an evening of splendid music and company. 5. The office Dibble band played well. 6. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@chenwy sam]$ sed "/honeysuck/c\The Office Dibble band played well." quote.txt 2. The Office Dibble band played well. 3. It was an evening of splendid music and company. 4. Too bad the disco floor fell through at 23:00. 5. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@chenwy sam]$ cat mix.sed 2. #!/bin/sed -f 3. 1 c\ 4. The Dibble band were grooving. 5. 6. /evening/ i\ 7. They played some great tunes. 8. 9. 3 a\ 10. Where was the nurse to help? 复制代码
1. [sam@chenwy sam]$ chmod u+x mix.sed 2. [sam@chenwy sam]$ ./mix.sed quote.txt 3. The Dibble band were grooving. 4. They played some great tunes. 5. It was an evening of splendid music and company. 6. Too bad the disco floor fell through at 23:00. 7. Where was the nurse to help? 8. The local nurse Miss P.Neave was in attendance. 复制代码
1. [ a d d r e s s [,a d d r e s s ] ] d 复制代码
1. [sam@chenwy sam]$ sed '1d' quote.txt 2. It was an evening of splendid music and company. 3. Too bad the disco floor fell through at 23:00. 4. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@chenwy sam]$ sed '1,3d' quote.txt 2. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@chenwy sam]$ sed '$d' quote.txt 2. The honeysuckle band played all night long for only $90. 3. It was an evening of splendid music and company. 4. Too bad the disco floor fell through at 23:00. 复制代码
1. [sam@chenwy sam]$ sed '/Neave/d' quote.txt 2. The honeysuckle band played all night long for only $90. 3. It was an evening of splendid music and company. 4. Too bad the disco floor fell through at 23:00. 复制代码
1. [ a d d r e s s [,address]] s/ pattern-to-find /replacement-pattern/[g p w n] 复制代码
g 缺省情况下只替换第一次出现模式,使用g选项替换全局所有出现模式。
1. [sam@chenwy sam]$ sed 's/night/NIGHT/' quote.txt 2. The honeysuckle band played all NIGHT long for only $90. 3. It was an evening of splendid music and company. 4. Too bad the disco floor fell through at 23:00. 5. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@chenwy sam]$ sed 's/\$//' quote.txt 2. The honeysuckle band played all night long for only 90. 3. It was an evening of splendid music and company. 4. Too bad the disco floor fell through at 23:00. 5. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@chenwy sam]$ sed 's/The/Wow!/g' quote.txt 2. Wow! honeysuckle band played all night long for only $90. 3. It was an evening of splendid music and company. 4. Too bad the disco floor fell through at 23:00. 5. Wow! local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@chenwy sam]$ sed 's/splendid/SPLENDID/w sed.out' quote.txt 2. The honeysuckle band played all night long for only $90. 3. It was an evening of SPLENDID music and company. 4. Too bad the disco floor fell through at 23:00. 5. The local nurse Miss P.Neave was in attendance. 复制代码
1. [sam@chenwy sam]$ cat sed.out 2. It was an evening of SPLENDID music and company. 复制代码 |