sed命令拷屏

http://blog.sina.com.cn/s/blog_45497dfa0100w6r3.html   sed样例较多,可以参考

http://blog.sina.com.cn/s/blog_6dc9e4cf0100y40g.html

sed各种修改文本用法总结

http://blog.csdn.net/ysdaniel/article/details/7905837

-e,-n,-i,-f
g,p,r,w,!,d,l,n
s//p,s//g,{;} [root@-shiyan sed]# cat -n grade.txt
M.Tansley / Green
J.Lulu / green
P.Bunny / Yellow
J.Troll / Brown-
L.Tansley / Brown-
###其中$d可以换成任何定址符号,例如逗号,正则,数字之类的
###删除尾行
[root@-shiyan sed]# sed '$d' grade.txt
M.Tansley / Green
J.Lulu / green
P.Bunny / Yellow
J.Troll / Brown-
###删除除green行的其它行,相当于取反。
[root@-shiyan sed]# sed '/green/!d' grade.txt
J.Lulu / green
###行后插入
[root@-shiyan sed]# sed '/green/a\\aaaaaaaaaaaa' grade.txt
M.Tansley / Green
J.Lulu / green
aaaaaaaaaaa
P.Bunny / Yellow
J.Troll / Brown-
L.Tansley / Brown-
###行前插入
[root@-shiyan sed]# sed '/green/i\\aaaaaaaaaaaa' grade.txt
M.Tansley / Green
aaaaaaaaaaa
J.Lulu / green
P.Bunny / Yellow
J.Troll / Brown-
L.Tansley / Brown-
###替换行
[root@-shiyan sed]# sed '/green/c\\aaaaaaaaaaaa' grade.txt
M.Tansley / Green
aaaaaaaaaaa
P.Bunny / Yellow
J.Troll / Brown-
L.Tansley / Brown-
[root@-shiyan sed]# sed '1,3y/99/87/' grade.txt
M.Tansley / Green
J.Lulu / green
P.Bunny / Yellow
J.Troll / Brown-
L.Tansley / Brown-
[root@-shiyan sed]# sed -n 's/$/&aa/p' grade.txt
M.Tansley / Green 44aa
J.Lulu / green 26aa
P.Bunny / Yellow 28aa
J.Troll / Brown- 26aa
L.Tansley / Brown- 28aa
c.oi / wo 4aa
###成功匹配并替换后写入aa.txt
[root@-shiyan sed]# sed -n 's/$/&aa/w aa.txt' grade.txt
###在green行后读入aa.txt文件。
[root@-shiyan sed]# sed '/green/r aa.txt' grade.txt
M.Tansley / Green
J.Lulu / green
# no packages own wtmpaaaaaaaaaaaaaaaaa and btmpaaaaaaaaaaaaaaaaa -- we'll rotate them here
/var/log/wtmpaaaaaaaaaaaaaaaaa {
create root utmpaaaaaaaaaaaaaaaaa
/var/log/btmpaaaaaaaaaaaaaaaaa {
create root utmpaaaaaaaaaaaaaaaaa
P.Bunny / Yellow
J.Troll / Brown-
L.Tansley / Brown-
c.oi / wo ###
[root@-shiyan sed]# sed -n '/file/,/root/s/file/iiiiiiiiiiiiiiii/p' logrotate.conf
# rotate log iiiiiiiiiiiiiiiis weekly
# create new (empty) log iiiiiiiiiiiiiiiis after rotating old ones
# use date as a suffix of the rotated iiiiiiiiiiiiiiii
# uncomment this if you want your log iiiiiiiiiiiiiiiis compressed
###默认只替换匹配到的第一个字符串,除非加g,全局替换。
[root@-shiyan sed]# sed -n 's/tmp/&aaaaaaaaaaaaaaaaa/p' logrotate.conf
# no packages own wtmpaaaaaaaaaaaaaaaaa and btmp -- we'll rotate them here
/var/log/wtmpaaaaaaaaaaaaaaaaa {
create root utmpaaaaaaaaaaaaaaaaa
/var/log/btmpaaaaaaaaaaaaaaaaa {
create root utmpaaaaaaaaaaaaaaaaa
###全局替换并打印,同时写入aa.txt文件。
[root@-shiyan sed]# sed -n 's/tmp/&aaaaaaaaaaaaaaaaa/gpw aa.txt' logrotate.conf
# no packages own wtmpaaaaaaaaaaaaaaaaa and btmpaaaaaaaaaaaaaaaaa -- we'll rotate them here
/var/log/wtmpaaaaaaaaaaaaaaaaa {
create root utmpaaaaaaaaaaaaaaaaa
/var/log/btmpaaaaaaaaaaaaaaaaa {
create root utmpaaaaaaaaaaaaaaaaa ###只对下一行操作
[root@-shiyan sed]# sed '/green/{n;s/12/9999/;}' grade.txt
M.Tansley / Green
J.Lulu / green
P.Bunny / Yellow
J.Troll / Brown-
L.Tansley / Brown-
E.Wo / bule

###对不连续行执行操作,即指定行的删除
[root@250-shiyan sed]# sed -i '1s/$/#&/;4s/$/#&/' grade.txt
[root@250-shiyan sed]# cat grade.txt
#
J.Lulu          06/99   48317   green   9       24      26
P.Bunny         02/99   48      Yellow  12      35      28
J.Troll         07/99   4842    Brown-3 12      26      26#
L.Tansley       05/99   4712    Brown-3 12      30      28
E.Wo            02/87   76      bule    34      88      5 ###针对不连续行操作
[root@250-shiyan sed]# sed '1d;3d;5d' grade.txt
J.Lulu          06/99   48317   green   9       24      26
J.Troll         07/99   4842    Brown-3 12      26      26#
E.Wo            02/87   76      bule    34      88      5
###标签函数应用,较难理解。定义标签p1,执行替换操作,当A或B到达3个时。
[root@-shiyan sed]# cat > file
A
B
[root@-shiyan sed]# sed '{:p1;/A/s/A/AA/;/B/s/B/BB/;/[AB]\{3\}/b;b p1;}' file
AAA
BBB ###列出非打印字符
[root@-shiyan sed]# sed -n 'l' grade.txt
M.Tansley\t05/\t48311\tGreen\t8\t40\t44$
J.Lulu\t\t06/\t48317\tgreen\t9\t24\t26$
P.Bunny\t\t02/\t48\tYellow\t12\t35\t28$
J.Troll\t\t07/\t4842\tBrown-\t12\t26\t26$
L.Tansley\t05/\t4712\tBrown-\t12\t30\t28$
E.Wo\t\t02/\t76\tbule\t34\t88\t5$ ###第三行打印完就退出
[root@250-shiyan sed]# sed '3q' logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly
其中:test_sed的内容是:

sed 'y/1234567890/ABCDEFGHIJ/' test_sed
ABCDEFGHIJ
BCDEFGHIJA
CDEFGHIJAB
DEFGHIJABC
注意变换关系是按两个list的位置对应变换 sed 's/01/Ab/g' test_sed 23456789Ab
3456789Ab2
456789Ab23
注意:第一行的0,1没有分别替换为A,b test.txt,内容如下:
http://www.5566.cn/produce/20070804112/315613171.shtml
http://bj.5566.cn/produce/20070804112/31581217.shtml
http://sz.5566.cn/produce/2008090412/31581247.shtml 要求如下:将http://*.5566.cn/替换成/home/html/www.5566.cn # sed -n 's/http.*\.cn/\/home\/html\/www.5566.cn/gp' test.txt
/home/html/www..cn/produce//.shtml
/home/html/www..cn/produce//.shtml
/home/html/www..cn/produce//.shtml
[root@-shiyan sed]# sed '3q' logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly -e选项允许在同一行里执行多条命令。
sed -e '1,5d' -e 's/test/check/'example
如例子所示,第一条命令删除1至5行,第二条命令用 check替换test。命令的执行顺序对结果有影响。如果两个命令都是替换命令,那么第一个替换命令将影响第二个替换命令的结果。
一个比-e更好的命令是--expression。它能给sed表达式赋值。
sed --expression='s/test/check/' --expression='/love/d' example
[root@manage ~]# cat qq
qqqqqqqqqqqq
ddddddddddd
wwwwwwwwwwww
在第二行插入换行与uuuu行。\n是换行
[root@manage ~]# sed -i '2i\\nuuuuuuuuu' qq
[root@manage ~]# cat qq
qqqqqqqqqqqq uuuuuuuuu
ddddddddddd
wwwwwwwwwwww

[root@manage ~]# cat qq
qqqqqqqqqqqq
uuuuuuuuu
ddddddddddd
wwwwwwwwwwww 在第三行后插入
[root@manage ~]# sed -i '3a\\n111111111' qq
[root@manage ~]# cat qq
qqqqqqqqqqqq
uuuuuuuuu
ddddddddddd wwwwwwwwwwww 将第三行替换
[root@manage ~]# sed -i '3c\22' qq
[root@manage ~]# cat qq
qqqqqqqqqqqq
uuuuuuuuu wwwwwwwwwwww 删除第4行到第6行
[root@manage ~]# sed -i '4,6d' qq
[root@manage ~]# cat qq
qqqqqqqqqqqq
uuuuuuuuu
22 一次插入多行
[root@manage ~]# cat qq
qqqqqqqqqqqq
aaa
bbb
ccc
uuuuuuuuu
22
[root@manage ~]# sed -i '2i\ 5a ee;\nbbb fff;\nccc  oooooo;' qq
[root@manage ~]# cat qq
qqqqqqqqqqqq
 5a ee;
bbb fff;
ccc  oooooo;
aaa
bbb
ccc
uuuuuuuuu
22
上一篇:CSS代码写出的各种形状图形


下一篇:java编程思想第四版中net.mindview.util包下载,及源码简单导入使用