免费注册 查看新帖 |

Chinaunix

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

vi里的\n,为什么在sed里不认那 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-12-18 13:08 |只看该作者 |倒序浏览
vi里
:%s/\n//g  可以
但是
sed '/\n//g file > result
就是不行

论坛徽章:
0
2 [报告]
发表于 2006-12-18 13:21 |只看该作者
\n 在用 sed 处理时候, 压根儿没有出现在 pattern space ...所以匹配不到.
但是用 N 命令时候 pattern space 会出现 \n 这时候就匹配了

论坛徽章:
0
3 [报告]
发表于 2006-12-20 14:03 |只看该作者
因为sed会去掉每个输入行的结尾的回车,具体参见下面的文章

5.10. Why can't I match or delete a newline using the \n escape sequence?
Why can't I match 2 or more lines using \n?

The \n will never match the newline at the end-of-line because the
newline is always stripped off before the line is placed into the
pattern space. To get 2 or more lines into the pattern space, use
the 'N' command or something similar (such as 'H;...;g;').

Sed works like this: sed reads one line at a time, chops off the
terminating newline, puts what is left into the pattern space where
the sed script can address or change it, and when the pattern space
is printed, appends a newline to stdout (or to a file). If the
pattern space is entirely or partially deleted with 'd' or 'D', the
newline is *not* added in such cases. Thus, scripts like

sed 's/\n//' file # to delete newlines from each line
sed 's/\n/foo\n/' file # to add a word to the end of each line

will _never_ work, because the trailing newline is removed _before_
the line is put into the pattern space. To perform the above tasks,
use one of these scripts instead:

tr -d '\n' < file # use tr to delete newlines
sed ':a;N;$!ba;s/\n//g' file # GNU sed to delete newlines
sed 's/$/ foo/' file # add "foo" to end of each line

Since versions of sed other than GNU sed have limits to the size of
the pattern buffer, the Unix 'tr' utility is to be preferred here.
If the last line of the file contains a newline, GNU sed will add
that newline to the output but delete all others, whereas tr will
delete all newlines.

To match a block of two or more lines, there are 3 basic choices:
(1) use the 'N' command to add the Next line to the pattern space;
(2) use the 'H' command at least twice to append the current line
to the Hold space, and then retrieve the lines from the hold space
with x, g, or G; or (3) use address ranges (see section 3.3, above)
to match lines between two specified addresses.

Choices (1) and (2) will put an \n into the pattern space, where it
can be addressed as desired ('s/ABC\nXYZ/alphabet/g'). One example
of using 'N' to delete a block of lines appears in section 4.13
("How do I delete a block of _specific_ consecutive lines?"). This
example can be modified by changing the delete command to something
else, like 'p' (print), 'i' (insert), 'c' (change), 'a' (append),
or 's' (substitute).

Choice (3) will not put an \n into the pattern space, but it _does_
match a block of consecutive lines, so it may be that you don't
even need the \n to find what you're looking for. Since several
versions of sed support this syntax:

sed '/start/,+4d' # to delete "start" plus the next 4 lines,

in addition to the traditional '/from here/,/to there/{...}' range
addresses, it may be possible to avoid the use of \n entirely.


不过这个pattern space 有大小的限制,如果文件很大,用tr来做替换工作是首选。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP