【一练】sed用法练习

上一篇 / 下一篇  2017-11-13 10:48:41 / 个人分类:shell

>>>cat datafile
Steve Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300
Betty Boop:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place,Caldwell,NJ 23875:6/18/68:23400
Norma Corder:322-222-222:3332 PIne Street,Dearborn,mi 2334:5/53/341:3344
Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300
Btty Boop:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500

任务1:将Boop替换为BOOP:
>>> sed -e 's/Boop/BOOP/g' datafile
Steve Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300
Betty BOOP:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place,Caldwell,NJ 23875:6/18/68:23400
Norma Corder:322-222-222:3332 PIne Street,Dearborn,mi 2334:5/53/341:3344
Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300
Btty BOOP:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500

任务2:删除前3行:
>>> sed -e '1,3d' datafile
Norma Corder:322-222-222:3332 PIne Street,Dearborn,mi 2334:5/53/341:3344
Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300
Btty Boop:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500

任务3:显示1-3行:
>>> sed -n -e '1,3p' datafile
Steve Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300
Betty Boop:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place,Caldwell,NJ 23875:6/18/68:23400

任务4:删除包含Boop行:
>>> sed -e '/Boop/d' datafile
Steve Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300
Igor Chevsky:385-375-8395:3567 Populus Place,Caldwell,NJ 23875:6/18/68:23400
Norma Corder:322-222-222:3332 PIne Street,Dearborn,mi 2334:5/53/341:3344
Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300

任务5:显示生日在5-6月之间的行:
ps:每行末为生日字段,如11/12/56为56年11月12日
>>> sed -n -e '/[:::][5,6][:/:]/p' datafile
Betty Boop:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place,Caldwell,NJ 23875:6/18/68:23400
Norma Corder:322-222-222:3332 PIne Street,Dearborn,mi 2334:5/53/341:3344
Btty Boop:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500
## ps :sed中特殊字符用[::]包起来,如特殊字符\则是用[:\:]表示
也可用以下命令:
>>> sed -n -e '/:6[^0-9]/,/:5[^0-9]/p' datafile

任务6:把三个星号(***)添加到以Steve开头的行:
>>> sed -e 's/^Steve/***Steve/g' datafile
***Steve Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300
Betty Boop:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place,Caldwell,NJ 23875:6/18/68:23400
Norma Corder:322-222-222:3332 PIne Street,Dearborn,mi 2334:5/53/341:3344
Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300
Btty Boop:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500

任务7:用C-O-R-D-E-R取代包含Corder的行:
>>> sed -e 's/^.*Corder.*$/C-O-R-D-E-R/g' datafile
Steve Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300
Betty Boop:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place,Caldwell,NJ 23875:6/18/68:23400
C-O-R-D-E-R
Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300
Btty Boop:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500

ps:也可用以下方法:
>>> sed -e '/Corder/s/.*/C-O-R-D-E-R/g' datafile

任务8:将Steve Blenheim的生日改为11/11/11:
>>> sed -e '/Steve Blenheim/s/:[0-9]*\/.*\/.*:/:11\/11\/11:/' datafile
Steve Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/11/11:20300
Betty Boop:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place,Caldwell,NJ 23875:6/18/68:23400
Norma Corder:322-222-222:3332 PIne Street,Dearborn,mi 2334:5/53/341:3344
Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300
Btty Boop:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500

任务9:删除所有空白行:
>>> sed -e '/^$/d' datafile

任务10:写一个脚本,在第一行之前插入Begin;在文件末尾添加The end;删除以3344结尾的记录;显示文件内容,把姓和名颠倒:
>>> cat handle.sed
/3344/d
1iBegin
$aThe end
s/^\([a-zA-Z]\+\) \([a-zA-Z]\+\)\(.*\)/\2 \1\3/g
>>> sed -f handle.sed datafile
Begin
Blenheim Steve:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300
Boop Betty:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500
Chevsky Igor:385-375-8395:3567 Populus Place,Caldwell,NJ 23875:6/18/68:23400
Blenheim:238-923-7366:95 Lantham Lane,Easton,PA 83755:11/12/56:20300
Boop Btty:245-836-8357:635 Cutesy Lane,Hollywood,CA 91464:6/23/23:14500
The end

TAG:

 

评分:0

我来说两句

Open Toolbar