LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1286|回复: 4

一个空行是否包含换行符?

[复制链接]
发表于 2008-9-30 08:59:05 | 显示全部楼层 |阅读模式
空行往往都是用^$表示,但是在行末是否包含一个换行符?如果包含一个换行符,那么为什么不表示为:^\n$
还有两个连续的空行应该如何表示?
发表于 2008-9-30 09:37:07 | 显示全部楼层
这个是一些基本的概念上问题。
对于windows系统来说,认为\r\n是“行分隔符”,他们不是行的一部分,是用来分隔两个行的标志,是两个独立的字符。所以按照windows的标准,完美检测两个连续的空行是做不到的,因为你不能确定第二个行后面是否还包含那个“行分隔符”。

对于Linux系统来说,认为\n是“行结束符”,\n就是行结尾本身,而不是行的最后一个字符。因此使用$搜索其实本质上就意味着匹配行结束符,不能再去显式匹配那个\n字符。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-9-30 09:46:26 | 显示全部楼层
$ cat mytest.sh
This line is followed by 1 blank line.

This line is followed by 2 blank line.


This line is followed by 3 blank line.



This line is followed by 4 blank line.




This is the end.

$ cat sed.debug
/^$/{
        N
        /^\n$/D           #这里为什么需要显示匹配\n呢?
}

$ sed -f sed.debug mytest.sh
This line is followed by 1 blank line.

This line is followed by 2 blank line.

This line is followed by 3 blank line.

This line is followed by 4 blank line.

This is the end.
回复 支持 反对

使用道具 举报

发表于 2008-10-13 20:41:22 | 显示全部楼层
这个问题,其实你 man一下就可以了。


这里首先要说明poet的说法是完全正确的。

主要是N命令不了解造成的。

  1. N
  2.              Append the next line of input to the pattern space, using an
  3.              embedded newline character to separate the appended material from
  4.              the original contents.  Note that the current line number
  5.              changes.
复制代码

由于N是表示把下一行附加到模式空间,sed会自动插入一个内嵌的\n 作为分隔符。
/^\n$/ 用来判断是不是两个空行。
回复 支持 反对

使用道具 举报

发表于 2008-10-14 14:32:11 | 显示全部楼层
debug information


  1. PATT:This line is followed by 1 blank line.$
  2. COMM:/^$/ {
  3. PATT:This line is followed by 1 blank line.$
  4. This line is followed by 1 blank line.
  5. PATT:$
  6. COMM:/^$/ {
  7. COMM:N
  8. PATT:\nThis line is followed by 2 blank line.$
  9. COMM:/^\n$/ D
  10. PATT:\nThis line is followed by 2 blank line.$
  11. COMM:}
  12. PATT:\nThis line is followed by 2 blank line.$

  13. This line is followed by 2 blank line.
  14. PATT:$
  15. COMM:/^$/ {
  16. COMM:N
  17. PATT:\n$
  18. COMM:/^\n$/ D
  19. PATT:$
  20. COMM:/^$/ {
  21. COMM:N
  22. PATT:\nThis line is followed by 3 blank line.$
  23. COMM:/^\n$/ D
  24. PATT:\nThis line is followed by 3 blank line.$
  25. COMM:}
  26. PATT:\nThis line is followed by 3 blank line.$

  27. This line is followed by 3 blank line.
  28. PATT:$
  29. COMM:/^$/ {
  30. COMM:N
  31. PATT:\n$
  32. COMM:/^\n$/ D
  33. PATT:$
  34. COMM:/^$/ {
  35. COMM:N
  36. PATT:\n$
  37. COMM:/^\n$/ D
  38. PATT:$
  39. COMM:/^$/ {
  40. COMM:N
  41. PATT:\nThis line is followed by 4 blank line.$
  42. COMM:/^\n$/ D
  43. PATT:\nThis line is followed by 4 blank line.$
  44. COMM:}
  45. PATT:\nThis line is followed by 4 blank line.$

  46. This line is followed by 4 blank line.
  47. PATT:$
  48. COMM:/^$/ {
  49. COMM:N
  50. PATT:\n$
  51. COMM:/^\n$/ D
  52. PATT:$
  53. COMM:/^$/ {
  54. COMM:N
  55. PATT:\n$
  56. COMM:/^\n$/ D
  57. PATT:$
  58. COMM:/^$/ {
  59. COMM:N
  60. PATT:\n$
  61. COMM:/^\n$/ D
  62. PATT:$
  63. COMM:/^$/ {
  64. COMM:N
复制代码
回复 支持 反对

使用道具 举报

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

本版积分规则

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