sed简单脚本练习

sed脚本的执行顺序可以简记为:Read,Execute,Print,Repeat(读取,执行,打印,重复)简称REPR

分析脚本的执行顺序:

l 读取一行到模式空间(sed内部的一个临时缓存,用于存放读取到的内容)

l 在模式空间中之心命令。如果使用了{ }或者-e指定了多个命令,sed将依次执行每个命令

l 打印模式空间的内容,然后清空模式空间

l 重复上述过程,直到文件结束

在sed选项中-n参数的作用是屏蔽sed的默认输出,p指定你输出的内容,请对比以下两个例子

[root@sishen ~]# sed 'p' employee.txt

101,John Doe,CEO

101,John Doe,CEO

102,Jason Smith,IT Manager

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

105,Jane Miller,Sales Manager

[root@sishen ~]# sed -n 'p' employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

指定输出范围

示例1 只打印第2行

[root@sishen ~]# sed -n '2p' employee.txt

102,Jason Smith,IT Manager

示例2 打印第1至第4行

[root@sishen ~]# sed -n '1,4p' employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

示例3 打印第2至最后一行

[root@sishen ~]# sed -n '2,$ p' employee.txt

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

[root@sishen ~]# sed -n '2,$p' employee.txt

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

注意空格是否有区别

加号+配合逗号使用,可以指定相对若干行,而不是绝对的几行,如n,+m表示第n行开始(含)后的m行

波浪号~也可以指定地址范围,它指定每次要跳过的行数,如n~m表示从第n行开始,每次跳过m行

l 1~2匹配1,3,5,7,···········

l 2~2匹配2,4,6,8,············

l 1~3匹配1,4,7,10,··········

l 2~3匹配2,5,8,11,··········

示例1 只打印奇数行

[root@sishen ~]# sed -n '1~2 p' employee.txt

101,John Doe,CEO

103,Raj Reddy,Sysadmin

105,Jane Miller,Sales Manager

示例2 打印匹配模式”Jane”的行

[root@sishen ~]# sed -n '/Jane/ p' employee.txt

105,Jane Miller,Sales Manager

打印第一次匹配Jason的行至第4行

[root@sishen ~]# sed -n '/Jason/, 4 p' employee.txt

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

注意如果开始的4行中,没有匹配到Jason,那么sed会打印第4行以后匹配的内容

示例2 打印从第一次匹配Raj的行,到最后的所有行

[root@sishen ~]# sed -n '/Raj/,$ p' employee.txt

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

打印自匹配Raj的行开始到匹配Jane的行之间的所有内容

[root@sishen ~]# sed -n '/Raj/, /Jane/ p' employee.txt

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

打印匹配Jason的行和其后面的两行

[root@sishen ~]# sed -n '/Jane/, +2 p' employee.txt

105,Jane Miller,Sales Manager

注意使用匹配模式后不能和步长(波浪号~)共用

[root@sishen ~]# sed -n '/John/~2 p' employee.txt

sed: -e expression #1, char 7: unknown command: `~'

使用d来删除行,需要注意的是它只删除模式空间的内容,和其他sed命令一样,不会修改原始文件的内容,如果不提供地址范围,sed默认删除所有行

[root@sishen ~]# sed 'd' employee.txt

[root@sishen ~]#

所以什么都不会输出

示例2 只删除第2行

[root@sishen ~]# sed '2d' employee.txt

101,John Doe,CEO

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

删除第1至第4行

[root@sishen ~]# sed '1,4d' employee.txt

105,Jane Miller,Sales Manager

删除第2行至最后一行

[root@sishen ~]# sed '2,$d' employee.txt

101,John Doe,CEO

只删除奇数行

[root@sishen ~]# sed '1~2d' employee.txt

102,Jason Smith,IT Manager

104,Anand Ram,Developer

只删除偶数行

[root@sishen ~]# sed '2~2d' employee.txt

101,John Doe,CEO

103,Raj Reddy,Sysadmin

105,Jane Miller,Sales Manager

删除匹配Manager的行

[root@sishen ~]# sed '/Manager/d' employee.txt

101,John Doe,CEO

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

删除从第一次匹配Jason的行至第4行

[root@sishen ~]# sed '/Jason/,4d' employee.txt

101,John Doe,CEO

105,Jane Miller,Sales Manager

如果开头的4行中没有匹配Jason的行,那么上述命令将删除第4行以后匹配Manager的行

删除第一次匹配Raj的行至最后一行

[root@sishen ~]# sed '/Raj/,$d' employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

删除第一次匹配Jason的行和紧跟其后的2行

[root@sishen ~]# sed '/Jason/,+2d' employee.txt

101,John Doe,CEO

105,Jane Miller,Sales Manager

注意同样 在使用匹配模式的时候不能和步长公用

删除所有的空行

[root@sishen ~]# sed '/^$/ d' employee.txt

删除所有注释的行(或者是以#开头的行)

[root@sishen ~]# sed '/^#/d' employee.txt

注意如果有多个命令,sed遇到命令d时会删除匹配的整行数据,其余的命令将无法操作被删除的行

把模式空间的内容写到文件中(w命令)

把employee.txt文件中的内容输出到output.txt文件中,同时显示在屏幕上

[root@sishen ~]# sed 'w output.txt' employee.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

[root@sishen ~]# sed -n 'w output1.txt' employee.txt

[root@sishen ~]# cat output1.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

[root@sishen ~]#

注意 加上-n参数就不会打印在屏幕上

只保存第2行以及保存第一至第四行

[root@sishen ~]# sed -n '2 w out2.txt ' employee.txt

[root@sishen ~]# sed -n '1,4 w out4.txt' employee.txt

[root@sishen ~]# cat out2.txt\

102,Jason Smith,IT Manager

[root@sishen ~]# cat out4.txt

101,John Doe,CEO

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

保存第2行至最后一行

[root@sishen ~]# sed -n '2,$ w outend.txt' employee.txt

[root@sishen ~]# cat outend.txt

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

只保存奇数行

[root@sishen ~]# sed -n '1~2 w out5.txt' employee.txt

[root@sishen ~]# cat out5.txt

101,John Doe,CEO

103,Raj Reddy,Sysadmin

105,Jane Miller,Sales Manager

保存匹配Jane的行

[root@sishen ~]# sed -n '/Jane/ w outj.txt' employee.txt

[root@sishen ~]# cat outj.txt

105,Jane Miller,Sales Manager

保存第一次匹配Jason的行至第4行

[root@sishen ~]# sed -n '/Jason/,4 w outJa.txt' employee.txt

[root@sishen ~]# cat outJa.txt

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

保存第一次匹配Raj的行至最后一行

[root@sishen ~]# sed -n '/Raj/,$ w out3.txt' employee.txt

[root@sishen ~]# cat out3.txt

保存匹配Raj的行至匹配Jane的行

[root@sishen ~]# sed -n '/Raj/,/Jane/ w out4.txt' employee.txt

[root@sishen ~]# cat out4.txt

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

105,Jane Miller,Sales Manager

注意:如果开始的4行里没有匹配到Jason,那么该命令只保存第4行以后匹配到Jason行

保存匹配Jason的行以及紧跟在其后的两行

[root@sishen ~]# sed -n '/Jason/,+2 w out6.txt' employee.txt

[root@sishen ~]# cat out6.txt

102,Jason Smith,IT Manager

103,Raj Reddy,Sysadmin

104,Anand Ram,Developer

posted on 2016-11-14 11:53  Lucky_7  阅读(446)  评论(0编辑  收藏  举报

导航