第11章:sed进阶操作
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选取等特定工作,下面先了解一下sed的用法
sed命令行格式为:
sed [option] ‘command’ 输入文本
[option] 常用选项:
-n --- 使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到萤幕上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
-e --- 直接在指令列模式上进行 sed 的动作编辑。
-f --- 直接将 sed 的动作写在一个档案内, -f filename 则可以执行 filename 内的sed 动作。
-r --- sed 的动作支援的是延伸型正规表示法的语法。(预设是基础正规表示法语法)。
-i --- 直接修改读取的档案内容,而不是由屏幕输出。
‘command’ 常用命令:
a ------ 新增
c ------ 取代
d ------ 删除
i ------ 插入
p ------ 列印
s ------ 替换
举例:
Linux系统创建了一个测试文本list,后续部分范例会使用到list测试文本,测试文本list内容如下:
[[email protected] home]# cat list
John Daggett, 341 King Road, Plymouth MA
Alice Ford, 22 EAST Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury MA
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston MA
范例1:’command’ 常用命令 s替换,把list文本MA字段替换成Massachusetts字段。
[[email protected] home]# sed ‘s/MA/Massachusetts/‘ list
John Daggett, 341 King Road, Plymouth Massachusetts
Alice Ford, 22 EAST Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury Massachusetts
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston Massachusetts
知识点补充:command’ 常用命令 s替换选项并不会影响list文本原来的内容。
范例2:[option] 常用选项 -e指定命令行上的多重指令。三种方法实现多重指令。
[[email protected] home]# sed ‘s/MA/,Massachusetts/;s/PA/,Pennsylvania/‘ list
John Daggett, 341 King Road, Plymouth ,Massachusetts
Alice Ford, 22 EAST Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls ,Pennsylvania
Eric Adams, 20 Post Road, Sudbury ,Massachusetts
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston ,Massachusetts
[[email protected] home]# sed -e ‘s/MA/,Massachusetts/‘ -e ‘s/PA/,Pennsylvania/‘ list
John Daggett, 341 King Road, Plymouth ,Massachusetts
Alice Ford, 22 EAST Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls ,Pennsylvania
Eric Adams, 20 Post Road, Sudbury ,Massachusetts
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston ,Massachusetts
[[email protected] home]# sed ‘
> s/MA/,Massachusetts/
> s/PA/,Pennsylvania/‘ list
John Daggett, 341 King Road, Plymouth ,Massachusetts
Alice Ford, 22 EAST Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls ,Pennsylvania
Eric Adams, 20 Post Road, Sudbury ,Massachusetts
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston ,Massachusetts
知识点补充:’command’并不是任何情况下都需要将指令用单引号括起来,但需要养成这个好习惯。使用单引号可以阻止shell解释编辑指令中的特殊字符或空格。
范例3:[option] 常用选项 -f直接将 sed 的动作写在一个文本内, -f 文本则可以执行文本内的sed 动作。
[[email protected] home]# cat filename
s/MA/,Massachusetts/
s/PA/,Pennsylvania/
s/CA/,California/
[[email protected] home]# sed -f filename list
John Daggett, 341 King Road, Plymouth ,Massachusetts
Alice Ford, 22 EAST Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls ,Pennsylvania
Eric Adams, 20 Post Road, Sudbury ,Massachusetts
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View ,California
Sal Carpenter, 73 6th Street, Boston ,Massachusetts
知识点补充:再次提醒,显示屏幕上的结果只是临时的,原文本list并没有发生改变。
范例4:[option] 常用选项 –n只打印经过sed 特殊处理的那一行(或者动作)才会被列出来,通常与’command’ 常用命令p打印出某个选择的资料。
[[email protected] home]# sed -n ‘s/MA/Massachusetts/p‘ list
John Daggett, 341 King Road, Plymouth Massachusetts
Eric Adams, 20 Post Road, Sudbury Massachusetts
Sal Carpenter, 73 6th Street, Boston Massachusetts
[[email protected] home]# cat -n list | sed - n ‘4p‘
4 Terry Kalkas, 402 Lans Road, Beaver Falls PA
[[email protected] home]# cat -n list | sed -n ‘1,4p‘
1 John Daggett, 341 King Road, Plymouth MA
2 Alice Ford, 22 EAST Broadway, Richmond VA
3 Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
4 Terry Kalkas, 402 Lans Road, Beaver Falls PA
[[email protected] home]# cat -n list | sed -n ‘1p;4p‘
1 John Daggett, 341 King Road, Plymouth MA
4 Terry Kalkas, 402 Lans Road, Beaver Falls PA
知识点补充:2,$p打印第二行到最后一行,$p打印最后一行
范例5:’command’ 常用命令d删除指定行
[[email protected] home]# cat -n list | sed ‘4d‘
1 John Daggett, 341 King Road, Plymouth MA
2 Alice Ford, 22 EAST Broadway, Richmond VA
3 Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
5 Eric Adams, 20 Post Road, Sudbury MA
6 Hubert Sims, 328A Brook Road, Roanoke VA
7 Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
8 Sal Carpenter, 73 6th Street, Boston MA
[[email protected] home]# cat -n list | sed ‘1,4d‘
5 Eric Adams, 20 Post Road, Sudbury MA
6 Hubert Sims, 328A Brook Road, Roanoke VA
7 Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
8 Sal Carpenter, 73 6th Street, Boston MA
[[email protected] home]# cat -n list | sed ‘1d;4d;8d‘
2 Alice Ford, 22 EAST Broadway, Richmond VA
3 Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
5 Eric Adams, 20 Post Road, Sudbury MA
6 Hubert Sims, 328A Brook Road, Roanoke VA
7 Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
知识点补充: $d删除最后一行,/^$/d删除空行,50,$d删除从50行到最后一行,