LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: johnny_jiang

[求助] 关于sed的命令 N,P,D

[复制链接]
 楼主| 发表于 2006-3-14 09:19:37 | 显示全部楼层

  1. bash# sed -e 'N;P' file
  2. one=1234
  3. one=1234
  4. two=ABCD
  5. three=1614.13
  6. three=1614.13
  7. four=734.25
  8. five=CDEF
  9. five=CDEF
  10. six=2586
  11. seven=EFGH
复制代码

但是pattern space还是会被output,如果pattern space中存在内容而且没有使用-n option,对吗?
回复 支持 反对

使用道具 举报

发表于 2006-3-14 10:15:20 | 显示全部楼层
当sed读到文件结束时,停止script处理,并打印模板:

[10:12:51 test]$ [color="SeaGreen"]sed 's/$/---header/;N;=;s/$/---tailer/' files
2
1. one---header
2. two---tailer
4
3. three---header
4. four---tailer
6
5. five---header
6. six---tailer
7. seven---header
回复 支持 反对

使用道具 举报

发表于 2006-3-14 13:34:56 | 显示全部楼层
Post by johnny_jiang

  1. bash# sed -e 'N;P' file
  2. one=1234
  3. one=1234
  4. two=ABCD
  5. three=1614.13
  6. three=1614.13
  7. four=734.25
  8. five=CDEF
  9. five=CDEF
  10. six=2586
  11. seven=EFGH
复制代码

但是pattern space还是会被output,如果pattern space中存在内容而且没有使用-n option,对吗?

if no "-n", pattern space and editing result will all be printed out.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-14 13:52:00 | 显示全部楼层
Post by yongjian
if no "-n", pattern space and hold space will all be printed out.


但是yongjian兄,为什么下面的命令返回和上面的一样呢



  1. bash# sed -e 'N;h;P' file
  2. one=1234
  3. one=1234
  4. two=ABCD
  5. three=1614.13
  6. three=1614.13
  7. four=734.25
  8. five=CDEF
  9. five=CDEF
  10. six=2586
  11. seven=EFGH

复制代码


最后的阶段,hold space中不是应该存有数据
five=CDEF
six=2586
:ask
回复 支持 反对

使用道具 举报

发表于 2006-3-14 15:02:17 | 显示全部楼层
保存空间应该是不打印的:

[14:59:13 test]$ sed '${h;N}' files
1. one
2. two
3. three
4. four
5. five
6. six
7. seven
[14:59:52 test]$ sed '${[color="Red"]x;N}' files
1. one
2. two
3. three
4. four
5. five
6. six

[15:00:08 test]$
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-14 15:19:07 | 显示全部楼层
个人同意linux_now兄的观点
回复 支持 反对

使用道具 举报

发表于 2006-3-15 03:35:05 | 显示全部楼层
Post by johnny_jiang
但是yongjian兄,为什么下面的命令返回和上面的一样呢



  1. bash# sed -e 'N;h;P' file
  2. one=1234
  3. one=1234
  4. two=ABCD
  5. three=1614.13
  6. three=1614.13
  7. four=734.25
  8. five=CDEF
  9. five=CDEF
  10. six=2586
  11. seven=EFGH

复制代码


最后的阶段,hold space中不是应该存有数据
five=CDEF
six=2586
:ask

Because  it hits EOF. When sed/awk hits EOF, process will stop immediately.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-15 08:45:16 | 显示全部楼层
Post by yongjian
Because  it hits EOF. When sed/awk hits EOF, process will stop immediately.


Yes, so my question is when it hits EOF, it just stops and does nothing or output the pattern space if any?

From the test, I think it sure outputs the pattern space and then exits.
回复 支持 反对

使用道具 举报

发表于 2006-3-15 11:37:36 | 显示全部楼层
Post by johnny_jiang
Yes, so my question is when it hits EOF, it just stops and does nothing or output the pattern space if any?

From the test, I think it sure outputs the pattern space and then exits.

From what I understand, the last several processes seem to be
1. hit five
2. apply N
3. read in six.
4. apply h
5. put five and six into hold space
6. apply P
7. print out the first part till newline which is five.
8. reach the end of script, since there is no "-n", then run default print. pattern space "five and six" two lines will be printed. so you see "five five six". pattern space gets cleaned.
9. read a new line in which is seven
10. apply N.
11. hit EOF. I thought sed should exit immediately w/o even printing out the pattern space, but looks like it does now. So I went to an AIX unix box and tried the same thing.
On AIX with old sed

  1. cat foo | sed -e 'N;P'
  2. one=1234
  3. one=1234
  4. two=ABCD
  5. three=1614.13
  6. three=1614.13
  7. four=734.25
  8. five=CDEF
  9. five=CDEF
  10. six=2586
复制代码


On Linux with newest sed

  1. cat foo | sed 'N;P'
  2. one=1234
  3. one=1234
  4. two=ABCD
  5. three=1614.13
  6. three=1614.13
  7. four=734.25
  8. five=CDEF
  9. five=CDEF
  10. six=2586
  11. seven=EFGH
复制代码

You can see the difference. When hitting EOF, the Linux ver looks like printing out the pattern space and then exit where old unix ver just exits immediately. Another comment that I made before is not correct, which was "without -n, pattern space and hold space will both be printed out". It should be "without -n, pattern space and the editing result will both be printed out" since output the pattern space is the default action of sed/awk. You guys are right, hold space will not be printed out.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-15 11:53:00 | 显示全部楼层
thx, yongjian. From your explanation

8. reach the end of script, since there is no "-n", then run default print. pattern space "five and six" two lines will be printed. so you see "five five six". pattern space and hold space get cleaned.


I think you mean after each turn of script, sed outputs the content of pattern space and then clean pattern space and hold space. But the test I did seems a little different from yours. Please see below


  1. bash# sed -e '1{ N;h }; 7x' file
  2. one=1234
  3. two=ABCD
  4. three=1614.13
  5. four=734.25
  6. five=CDEF
  7. six=2586
  8. one=1234
  9. two=ABCD
复制代码


You can see until last loop, hold space is filled with the content
one=1234
two=ABCD

Maybe it's another difference between some linux distribution.

I did my test under redhat and suse.
回复 支持 反对

使用道具 举报

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

本版积分规则

快速回复 返回顶部 返回列表