1.Sed 介绍
sed全名叫stream deitor,流编辑器。作为shell文本处理三剑客之一的sed,其不同于vi等全屏编辑器,sed编辑器没有提供交互式的使用方式,使用者只能在命令行输入编辑命令对文本处理,其处理方式为一次处理一行内容。sed基本上与grep搭配使用,所以要学好sed,首先得对grep相当熟练,方才能更好的掌握sed,成为你手中的利器。
2.Sed 格式命令用法
2.1 用法:
sed选项... [SCRIPT] [INPUTFILE ...]
常用选项:
-n:不输出模式空间内容到屏幕,即不自动打印
-e:多点编辑
-f:/PATH/SCRIPT_FILE:从指定文件中读取编辑脚本
-r:支持使用扩展表达式
-i.bak:备份文件并原处编辑
2.2 Sed 编辑命令:
- i\ text:在行前面插入文本
-a\ text:在指定一行后附加文本
-c\ text:替换行为单行或多行文本
-r:使用扩展正则
-s:用一个字符串替换为另一个
-p:打印当前模式空间内容,追加到默认输出后
-w /path/somefile:保存模式匹配的行至指定文件
-=:为模式空间中的行打印行号
-!:模式空间中匹配行取反处理
-r /path/somefile:读取指定文件的文本至模式空间中匹配到的行后
2.3 替换标志:
-p:打印行
-w:将行写入文件
-g:在行内进行全局替换
-x:交换暂存缓冲区与模式空间的内容
-y:将字符转换为另一个字符(不能对正则表达式使用y命令)
3. Sed 范例
3.1 实例文件
下面展示如何使用sed,包括如何使用它的选项、命令和搭配正则表达式。
下面用这段文本来演示:
3.2 打印:p 命令
命令p是打印命令,用于显示模式缓存区的内容,默认情况下,sed把输入行打印两遍在屏幕上,此时我们加上选项-n只打印包含模式的行,sed就可打印选定内容。
[root@CentOS6 /app]#sed -n '/shaohau/p' sedtest.txt
my son's name is shaohau
3.3 删除:d 命令
命令d用于删除输入行,sed先将输入行从文件复制到模式缓存区,然后对该行执行sed命令,最后将模式缓存区的内容显示屏幕上。如果发出的是命令d,当前模式缓存区的输入行会被删除,不被显示。
[root@CentOS6 /app]#sed '3d' sedtest.txt |nl
1 This is son
2 my son's name is shaohau
3 my dog's name is heimu
4 This is my brother
5 my brother's name is shaohui
6 This is my book
7 my book's name is linux
[root@CentOS6 /app]#nl sedtest.txt
1 This is son
2 my son's name is shaohau
3 This is my dog
4 my dog's name is heimu
5 This is my brother
6 my brother's name is shaohui
7 This is my book
8 my book's name is linux
[root@CentOS6 /app]#
3.4 替换:s 命令
命令s是替换命令。替换和取代文件中的文本通过sed中的s来实现,s后包含在斜杠中的文本是正则表达式,后面是需要替换的文本。通过g标志对行进行全局替换。
[root@CentOS6 /app]#nl sedtest.txt
1 This is son
2 my son's name is shaohau
3 This is my dog
4 my dog's name is heimu
5 This is my brother
6 my brother's name is shaohui
7 This is my book
8 my book's name is linux
[root@CentOS6 /app]#sed 's/This/My/g' sedtest.txt |nl
1 My is son
2 my son's name is shaohau
3 My is my dog
4 my dog's name is heimu
5 My is my brother
6 my brother's name is shaohui
7 My is my book
8 my book's name is linux
2.5 追加:a 命令
命令a 用于追加,将新文本追加到文件中当前行(即读入模式的缓存区行)的后面。
[root@CentOS6 /app]#nl sedtest.txt
1 This is son
2 my son's name is shaohau
3 This is my dog
4 my dog's name is heimu
5 This is my brother
6 my brother's name is shaohui
7 This is my book
8 my book's name is linux
[root@CentOS6 /app]#sed '/^This/a Hello,World' sedtest.txt |nl
1 This is son
2 Hello,World
3 my son's name is shaohau
4 This is my dog
5 Hello,World
6 my dog's name is heimu
7 This is my brother
8 Hello,World
9 my brother's name is shaohui
10 This is my book
11 Hello,World
12 my book's name is linux
3.6 修改:c 命令
c 命令是修改命令,sed使用该命令修改当前所匹配到的行。旧文本被覆盖。
[root@CentOS6 /app]#nl sedtest.txt
1 This is son
2 my son's name is shaohau
3 This is my dog
4 my dog's name is heimu
5 This is my brother
6 my brother's name is shaohui
7 This is my book
8 my book's name is linux
[root@CentOS6 /app]#sed '/my/c who\' sedtest.txt |nl
1 This is son
2 who
3 who
4 who
5 who
6 who
7 who
8 who
3.7 插入:i 命令
i 命令是插入命令,类似于 a 命令,但不是在当前行后增加文本,而是在当前行前面插进入新的文本。
[root@CentOS6 /app]#nl sedtest.txt
1 This is son
2 my son's name is shaohau
3 This is my dog
4 my dog's name is heimu
5 This is my brother
6 my brother's name is shaohui
7 This is my book
8 my book's name is linux
[root@CentOS6 /app]#sed '/This/i Helo.World!\ ---------' sedtest.txt
Helo.World! ---------
This is son
my son's name is shaohau
Helo.World! ---------
This is my dog
my dog's name is heimu
Helo.World! ---------
This is my brother
my brother's name is shaohui
Helo.World! ---------
This is my book
my book's name is linux
3.8 写入文件:w 命令
w 命令是写入文件命令,将文本实例中匹配到的行都写入到file里。
[root@CentOS6 /app]#nl sedtest.txt
1 This is son
2 my son's name is shaohau
3 This is my dog
4 my dog's name is heimu
5 This is my brother
6 my brother's name is shaohui
7 This is my book
8 my book's name is linux
[root@CentOS6 /app]#sed -n '/book/w /app/sedtest2.txt' sedtest.txt
[root@CentOS6 /app]#nl sedtest2.txt
1 This is my book
2 my book's name is linux