免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1060 | 回复: 3
打印 上一主题 下一主题

请教sed的一个问题,急救呀!!!!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-05-26 20:10 |只看该作者 |倒序浏览
书上说-e后面的命令是对同一行执行,那么为什么我下面的命令会产生不同的结果呢?迷茫中,大虾指点一二
datafile如下
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

sed -e '/Patricia/h'  -e '/Margot/x' -e '/AM/x' datafile
southeast     SE    Patricia Hemenway  4.0   .7   4   17
eastern       EA    TB Savage          4.4   .84  5   20
southeast     SE    Patricia Hemenway  4.0   .7   4   17
north        NO    Margot Weber       4.5   .89  5   9
central       CT    Ann Stephens       5.7   .94  5   13
(此命令产生结果与我想象有别:( )
sed -e '/Patricia/h' -e '/AM/x' -e '/Margot/x' datafile
southeast     SE    Patricia Hemenway  4.0   .7   4   17
eastern       EA    TB Savage          4.4   .84  5   20
southeast     SE    Patricia Hemenway  4.0   .7   4   17
northeast     NE    AM Main Jr.        5.1   .94  3   13
central       CT    Ann Stephens       5.7   .94  5   13
(此顺序产生结果正是我想见到的:)  )
包含AM和Margot的行不是一行那么后两个-e命令顺序不是随意吗?大侠指点一二
而且命令sed -e '/Patricia/h'  -e '/Margot/x' -e '/AM/g' datafile会产生结果
southeast     SE    Patricia Hemenway  4.0   .7   4   17
eastern       EA    TB Savage          4.4   .84  5   20
southeast     SE    Patricia Hemenway  4.0   .7   4   17
southeast     SE    Patricia Hemenway  4.0   .7   4   17
central       CT    Ann Stephens       5.7   .94  5   13
这和想象结果一样,但是更让我不明白为什么第一种命令顺序的Margot行没与
保持缓冲区交换呢?大侠救命呀!!!!!!!!!

论坛徽章:
0
2 [报告]
发表于 2004-05-27 11:40 |只看该作者

请教sed的一个问题,急救呀!!!!!!

-e 是順序的.
我把datafile加了數字,方便分析.
原數據內容
# cat datafile
1 southeast SE Patricia Hemenway 4.0 .7 4 17
2 eastern EA TB Savage 4.4 .84 5 20
3 northeast NE AM Main Jr. 5.1 .94 3 13
4 north NO Margot Weber 4.5 .89 5 9
5 central CT Ann Stephens 5.7 .94 5 13


# sed -e '/^1/h' -e '/^4/x' -e '/^3/x' datafile
1 southeast SE Patricia Hemenway 4.0 .7 4 17
2 eastern EA TB Savage 4.4 .84 5 20
1 southeast SE Patricia Hemenway 4.0 .7 4 17
4 north NO Margot Weber 4.5 .89 5 9
5 central CT Ann Stephens 5.7 .94 5 13
分析
line 1:
  before: current=line1
  '^1/h' true. put  line 1 to buffer,so  buffer=line1
  '^4/x' , '/^3/x' false
  result: output line1 and buffer=line1
line 2:
  before: current=line2
  current=line2
  '^1/h' ..... false, so do nothing.
  result: output line2 and buffer=line1
line 3:
  before: current=line3
  '^1/h' false
  '^4/x' false
  '^3/x' true, exchange current and buffer , so current=line1 and buffer=line3
   result: output line1 and buffer=line3
line 4:
before:current=line4
  '^1/h' false
  '^4/x' true , exchange current and buffer,so current=line3, buffer=line4
  '^3/x' true, 因為上一步已經把current=line3. after exchange current and buffer, current=line4 and buffer=line3
   resultutput line4 and buffer=line3
line 5;
    same as line 2
    result utput line 5

# sed -e '/^1/h' -e '/^3/x' -e '/^4/x' datafile
1 southeast SE Patricia Hemenway 4.0 .7 4 17
2 eastern EA TB Savage 4.4 .84 5 20
1 southeast SE Patricia Hemenway 4.0 .7 4 17
3 northeast NE AM Main Jr. 5.1 .94 3 13
5 central CT Ann Stephens 5.7 .94 5 13
分析
line 1:
  before: current=line1
  '^1/h' true. put  line 1 to buffer,so  buffer=line1
  '^3/x' , '/^4/x' false
  result: output line1 and buffer=line1
line 2:
  before: current=line2
  current=line2
  '^1/h' ..... &&³&false, so do nothing.
  result: output line2 and buffer=line1
line 3:
  before: current=line3
  '^1/h' false
  '^3/x' true, exchange current and buffer, so current=line1 and buffer=line3
  '^3/x' false
   result: output line1 and buffer=line3
line 4:
before:current=line4
  '^1/h' false
  '^3/x' false
  '^4/x' true, exchange current and buffer, current=line3 and buffer=line4
   resultutput line3 and buffer=line4
line 5;
    same as line 2
    result utput line 5


# sed -e '/^1/h' -e '/^4/x' -e '/^3/g' datafile
1 southeast SE Patricia Hemenway 4.0 .7 4 17
2 eastern EA TB Savage 4.4 .84 5 20
1 southeast SE Patricia Hemenway 4.0 .7 4 17
1 southeast SE Patricia Hemenway 4.0 .7 4 17
5 central CT Ann Stephens 5.7 .94 5 13
Do you explain ?

论坛徽章:
0
3 [报告]
发表于 2004-05-27 12:53 |只看该作者

请教sed的一个问题,急救呀!!!!!!

非常感谢,我明白了:)
是我想的太少谢谢谢谢

论坛徽章:
0
4 [报告]
发表于 2004-05-27 14:34 |只看该作者

请教sed的一个问题,急救呀!!!!!!

精辟....
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP