【shell】读《Unix shell 范例精解》–sed

 

【shell】读《Unix shell 范例精解》–sed

 

http://www.chenqing.org/booknotes-unixshell-sed.html

 

1、sed 的RE与grep的RE不同的是,sed将模式放在了斜杠间。
2、打印命令p
默认情况下,sed会将所有的输入行都打印在标准输出上。如果在某一行找到了匹配行,则将
该行在打印一遍(在匹配行的下面)
与 -n 选项一块则只打印匹配的行(-n 就是取消默认打印的功能的)
3、删除命令d
删除输入行,或者匹配的行
4、替换命令s
s/old/new/g
如果不加g,则替换每行的第一个
当然你也可以使用其它的作为分割符,紧跟在s后面的就是。
5、指定行的范围:逗号
‘/1/,/$/p’
6、 多重编辑命令e
-e ‘1,5d’ -e ‘s/old/new/g’
7、读文件r
将一个文本文件中的内容加到当前文件的特定位置
’/find/r file ‘ otherfile
即将在otherfile中找到匹配find,并在find的后面添加上file中的内容
8、写文件w
将当前文件所匹配的行写入到新文件中(会覆盖掉文件中的内容)
’/find/w file ‘ otherfile
9、追加a
a的命令之后要加一个“\”,要追加的的文本必须出现在追加命令的下一行,如果有多行追加文本,则每行的文本后都要加 ‘\’
[root@chenqing chap05]# sed  ‘/444/a\
—-hello—’ data

iddd    5555
isff    4444
—-hello—
iddd    5555
isff    4444
—-hello—
iddd    5555
isff    4444
—-hello—
iddd    5555
isff    4444
—-hello—
10、插入i
相对于a,在行前插入文本行
11、修改c
将匹配的那一行更换成新的文本
sed ‘/find/c\
—-hello—–’ file
12、获取下一行n
将匹配的那一行的下一行执行相关的操作
sed ‘/find/{ n; s/old/new/; }’ file
13、退出q
退出sed程序,不继续执行其它操作了
14、暂存和取用h,G(追加)g(替换)
sed -e ‘/find/h’ -e ‘$G’ file(追加在文件的行尾)
sed -e ‘/find/h’ -e ‘$g’ file(替换掉最后一行)
15、暂存和互换h,x
感觉和g的功能差不多

来个例子
[root@chenqing chap05]# cat datebook

Steve Blenheim:238-923-7366:95 Latham 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:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92743:7/25/53:85100
Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900
Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900
Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500
Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700
James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500
Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500
Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000
Zippy Pinhead:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500
Arthur Putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000
Popeye Sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350
Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600
Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
Yukio Takeshida:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000
Vinh Tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900

要求 名 和姓互换
[root@chenqing chap05]# sed ‘s/\(^[A-Z][a-z]*\) \([A-Z][a-z]*\):/\2 \1:/’ datebook

Blenheim Steve:238-923-7366:95 Latham 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
Corder Norma:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
Cowan Jennifer:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
Evich Karen:284-758-2857:23 Edgecliff Place, Lincoln, NB 92743:7/25/53:85100
Evich Karen:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
Evich Karen:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
Fardbarkle Fred:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900
Fardbarkle Fred:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900
Gortz Lori:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200
Gutierrez Paco:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500
Hardy Ephram:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700
Ikeda James:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Kertz Barbara:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500
Kirstin Lesley:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600
Kopf William:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500
Lancelot Sir:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500
Neal Jesse:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000
Pinhead Zippy:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500
Putie Arthur:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000
Sailor Popeye:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350
Santiago Jose:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600
Savage Tommy:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200
Takeshida Yukio:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000
Tranh Vinh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值