Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1348505
  • 博文数量: 112
  • 博客积分: 7112
  • 博客等级: 少将
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-31 16:27
文章分类

全部博文(112)

文章存档

2011年(19)

2010年(20)

2009年(16)

2008年(20)

2007年(37)

分类: LINUX

2007-09-20 17:37:41

 1.       打印:p

[root@TestAs4 chap04]# cat datafile               原文件

northwest       NW      Charles Main            3.0     .98     3       34

western         WE      Sharon Gray             5.3     .97     5       23

southwest       SW      Lewis Dalsass           2.7     .8      2       18

southern        SO      Suan Chin               5.1     .95     4       15

southeast       SE      Patricia Hemenway       4.0     .7      4       17

eastern         EA      TB Savage               4.4     .84     5       20

northeast       NE      AM Main Jr.             5.1     .94     3       13

north           NO      Margot Weber            4.5     .89     5        9

central         CT      Ann Stephens            5.7     .94     5       13

 

[root@TestAs4 chap04]# sed  -n '/north/p' datafile   取消默认输出 只打印包含模板的行

northwest       NW      Charles Main            3.0     .98     3       34

northeast       NE      AM Main Jr.             5.1     .94     3       13

north           NO      Margot Weber            4.5     .89     5        9

 

[root@TestAs4 chap04]# sed '/north/p' datafile       打印包含模板的行及打印默认输出

northwest       NW      Charles Main            3.0     .98     3       34

northwest       NW      Charles Main            3.0     .98     3       34

western         WE      Sharon Gray             5.3     .97     5       23

southwest       SW      Lewis Dalsass           2.7     .8      2       18

southern        SO      Suan Chin               5.1     .95     4       15

southeast       SE      Patricia Hemenway       4.0     .7      4       17

eastern         EA      TB Savage               4.4     .84     5       20

northeast       NE      AM Main Jr.             5.1     .94     3       13

northeast       NE      AM Main Jr.             5.1     .94     3       13

north           NO      Margot Weber            4.5     .89     5        9

north           NO      Margot Weber            4.5     .89     5        9

central         CT      Ann Stephens            5.7     .94     5       13

 

2.       删除:d

   [root@TestAs4 chap04]# sed '3d'  datafile            删除第三行

northwest       NW      Charles Main            3.0     .98     3       34

western         WE      Sharon Gray             5.3     .97     5       23

southern        SO      Suan Chin               5.1     .95     4       15

southeast       SE      Patricia Hemenway       4.0     .7      4       17

eastern         EA      TB Savage               4.4     .84     5       20

northeast       NE      AM Main Jr.             5.1     .94     3       13

north           NO      Margot Weber            4.5     .89     5        9

central         CT      Ann Stephens            5.7     .94     5       13

 

[root@TestAs4 chap04]# sed '3,$d'  datafile          删除第三行到最后的所有行

northwest       NW      Charles Main            3.0     .98     3       34

western         WE      Sharon Gray             5.3     .97     5       23

 

[root@TestAs4 chap04]# sed '/north/d' datafile        删除所有包含模板north的行

western         WE      Sharon Gray             5.3     .97     5       23

southwest       SW      Lewis Dalsass           2.7     .8      2       18

southern        SO      Suan Chin               5.1     .95     4       15

southeast       SE      Patricia Hemenway       4.0     .7      4       17

eastern         EA      TB Savage               4.4     .84     5       20

central         CT      Ann Stephens            5.7     .94     5       13

 

3.       选定行的范围:逗号

    [root@TestAs4 chap04]# sed -n '/west/,/east/p' datafile     所有在模板westeast所确定的行都被打印

northwest       NW      Charles Main            3.0     .98     3       34

western         WE      Sharon Gray             5.3     .97     5       23

southwest       SW      Lewis Dalsass           2.7     .8      2       18

southern        SO      Suan Chin               5.1     .95     4       15

southeast       SE      Patricia Hemenway       4.0     .7      4       17

 

 

[root@TestAs4 chap04]# sed -n '1,5'p datafile           打印第一、五行的内容

northwest       NW      Charles Main            3.0     .98     3       34

western         WE      Sharon Gray             5.3     .97     5       23

southwest       SW      Lewis Dalsass           2.7     .8      2       18

southern        SO      Suan Chin               5.1     .95     4       15

southeast       SE      Patricia Hemenway       4.0     .7      4       17

 

[root@TestAs4 chap04]# sed  '/west/,/east/s/$/**?VACA**/' datafile   对于eastwest之间的行,末尾用**?VACA**替换

northwest       NW      Charles Main            3.0     .98     3       34**?VACA**

western         WE      Sharon Gray             5.3     .97     5       23**?VACA**

southwest       SW      Lewis Dalsass           2.7     .8      2       18**?VACA**

southern        SO      Suan Chin               5.1     .95     4       15**?VACA**

southeast       SE      Patricia Hemenway       4.0     .7      4       17**?VACA**

eastern         EA      TB Savage               4.4     .84     5       20

northeast       NE      AM Main Jr.             5.1     .94     3       13

north           NO      Margot Weber            4.5     .89     5        9

central         CT      Ann Stephens            5.7     .94     5       13

 

[root@TestAs4 chap04]# sed  -n '/west/,/south/p' datafile                 

northwest       NW      Charles Main            3.0     .98     3       34

western         WE      Sharon Gray             5.3     .97     5       23

southwest       SW      Lewis Dalsass           2.7     .8      2       18

4.多点编辑:e命令

[root@TestAs4 chap04]# sed -e '1,3d' -e 's/Hemenway/Jones/' datafile    删除13行,用Hemenway替换Jones

southern        SO      Suan Chin               5.1     .95     4       15

southeast       SE      Patricia Jones  4.0     .7      4       17

eastern         EA      TB Savage               4.4     .84     5       20

northeast       NE      AM Main Jr.             5.1     .94     3       13

north           NO      Margot Weber            4.5     .89     5        9

central         CT      Ann Stephens            5.7     .94     5       13

 

 

5. 从文件读入:r 命令

[root@TestAs4 chap04]# cat newfile

        nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn

        | ***SUAN HAS LEFT THE COMPANY*** |

        |_________________________________|

 

 

[root@TestAs4 chap04]# sed  '/Suan/r newfile'  datafile       newfile文件内容放到Suan行的下面

northwest       NW      Charles Main            3.0     .98     3       34

western         WE      Sharon Gray             5.3     .97     5       23

southwest       SW      Lewis Dalsass           2.7     .8      2       18

southern        SO      Suan Chin               5.1     .95     4       15

        nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn

        | ***SUAN HAS LEFT THE COMPANY*** |

        |_________________________________|

southeast       SE      Patricia Hemenway       4.0     .7      4       17

eastern         EA      TB Savage               4.4     .84     5       20

northeast       NE      AM Main Jr.             5.1     .94     3       13

north           NO      Margot Weber            4.5     .89     5        9

central         CT      Ann Stephens            5.7     .94     5       13

 

注:如果不止一个Suan newfile的内容就将显示在所有匹配行的下面

 

6. 写入文件:w命令

[root@TestAs4 chap04]# sed  -n '/north/w  newfile2'  datafile   命令w表示把所有包含north的行写入到newfile2

 

[root@TestAs4 chap04]# cat newfile2

northwest       NW      Charles Main            3.0     .98     3       34

northeast       NE      AM Main Jr.             5.1     .94     3       13

north           NO      Margot Weber            4.5     .89     5        9

 

 

 

 

7. 追加:a 命令

[root@TestAs4 chap04]#  sed '/^north/a ---->THE NORTH SALES DISTRICT HAS MOVED<---' datafile   

northwest       NW      Charles Main            3.0     .98     3       34

---->THE NORTH SALES DISTRICT HAS MOVED<---

western         WE      Sharon Gray             5.3     .97     5       23

southwest       SW      Lewis Dalsass           2.7     .8      2       18

southern        SO      Suan Chin               5.1     .95     4       15

southeast       SE      Patricia Hemenway       4.0     .7      4       17

eastern         EA      TB Savage               4.4     .84     5       20

northeast       NE      AM Main Jr.             5.1     .94     3       13

---->THE NORTH SALES DISTRICT HAS MOVED<---

north           NO      Margot Weber            4.5     .89     5        9

---->THE NORTH SALES DISTRICT HAS MOVED<---

central         CT      Ann Stephens            5.7     .94     5       13

注: 在出现首个单词是north的后一行追加---->THE NORTH SALES DISTRICT HAS MOVED<---'

 

8. 插入: i 命令

[root@TestAs4 chap04]# sed '/eastern/i\

> NEW ENGLAND REGION\

> -------------------------------------' datafile

northwest       NW      Charles Main            3.0     .98     3       34

western         WE      Sharon Gray             5.3     .97     5       23

southwest       SW      Lewis Dalsass           2.7     .8      2       18

southern        SO      Suan Chin               5.1     .95     4       15

southeast       SE      Patricia Hemenway       4.0     .7      4       17

NEW ENGLAND REGION

-------------------------------------

eastern         EA      TB Savage               4.4     .84     5       20

northeast       NE      AM Main Jr.             5.1     .94     3       13

north           NO      Margot Weber            4.5     .89     5        9

central         CT      Ann Stephens            5.7     .94     5       13

 

注:如果模板eastern被匹配,i命令把反斜杠后面的文本插入到包含eastern的行的前面

 

 

9. 替换:s 命令

 

[root@TestAs4 oracle]# pwd

/u01/app/oracle

[root@TestAs4 oracle]# pwd  | sed  's/\/[^\/]*$/old/'      把“/ u01/app/oracle”的 /oracle”替换为old

/u01/appold

[root@TestAs4 chap04]# sed -n 's/Hemenway/Jones/pg' datafile      所有的Hemenway行被Jones 替换并打印

southeast       SE      Patricia Jones  4.0     .7      4       17

 


10. sed 用正则表达式

echo "aaaaaaaabbbbbbbbccccccccdddddddd"|sed 's/.\{8\}/&\n/g'

aaaaaaaa
         bbbbbbbb
         cccccccc
         dddddddd



=====================================================================

最近碰到一个处理跨行xml节点的问题,终于激发了进一步学习sed的愿望,并学会了使用b label写循环


        attr3 = "val3"
        attr4 = "val4"/>

替换指定xml节点中的attr4属性名
sed -e ‘//! {N; b loop}; s/attr4/ak47/ }’ test.xml


=======================================================================================
sed -e ‘//! {N; b loop}; s/attr4/ak47/ }’ test.xml

比如id="0"
当适配id="0"时开始进入{ :loop />/! {N; b loop}
:loop 是定义一个标签名为loop  这个标签脚本为 />/! {N; b loop}
/>/! 意思是当不适配>时执行{N; b loop}
N; 是增加下一行到模板直到遇到>为止 否则一直循环下去 然后把attr4替换为ak47

一般添加标签作为循环 主要是解决跨行问题。

 


==========================================

原文件如下:
select abc form dbs where a=b and b=c
select abc
form dbs
where
a=b and b=c
select
dbs
where
a=b
and
b=c
select * from dbs where a-b

要删除包含where a=b and b=c的行(也就是删除红字的行),但有可能是二行或是三行组成的。
结果如下:
select abc
form dbs
select
dbs
select * from dbs where a-b

因为包含的内容行数不固定用awk处理起来比较难,不知道sed可不可以处理,谢谢了!


sed '/where/{:a;s/[ \n]*where[ \n]*a=b[ \n]*and[ \n]*b=c//;tb;/\nwhere/{P;D;};N;ba;:b;d}' urfile


:a  冒号用来定义标签,名字可以自定义。:a定义标签a,用于指令跳转,循环处理。
tb t命令会在s///成功后跳到b标签;失败则不跳转,继续后面的指令。省略标签时,跳到脚本末尾,亦即开始处理下一行
ba 是无条件跳转到标签a。省略标签a则到脚本末尾。


[root@modem ~]# more 2
djjfkdjfk
Adkfjdkfj
fqqkd~
love1
kfjie~
love2
kdf~
love3
kdjfkdj
dkfjidj
dkfjiejfk
dkfjie

[root@modem ~]# sed 'N;:b;/\nlove/s/\n//;tb;P;D'   2
djjfkdjfk
Adkfjdkfj
fqqkd~love1
kfjie~love2
kdf~love3
kdjfkdj
dkfjidj
dkfjiejfk
dkfjie



阅读(2021) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~