什么是换行符——' ' [英] What is newline character -- ' '

查看:29
本文介绍了什么是换行符——' '的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常基本的概念,但我一直无法很好地表达.我想尝试拼写它,看看我哪里出错了.

This is a very basic concept, but something I have never been able to articulate that well. and I would like to try to spell it and see where I go wrong.

如果必须的话,我将如何定义换行符".假设我在unix(或windows)中创建了一个新文件,那么该文件是否通过在文件中插入一个称为换行符"的特殊字符来存储行尾"信息.如果是这样,它的 ascii 值是多少?我记得在 C 程序中,我已经根据值 ' ' 检查了读取字符.以及为什么这会混淆 2 个字符来表示行尾字符..

If I have to, how would I define a "newline character". say if I create a new file in unix(or windows), then does the file store the "end of line" information by inserting a special character in the file called as "new line character". If so, what is its ascii value? I remember that in C programs, I have checked for the read character against the value ' ' . And why this confusing 2 characters to represent end of line characters..

bash$ cat states
California
Massachusetts
Arizona

比如说,我想在两行之间插入一个行间距,并希望得到以下形式的输出:期望输出:

Say, I want to insert one line space between the lines and want an output of the form: Desired output:

California

Massachusetts

Arizona

bash$sed -e 's/
/

/g' states  does not work.

为什么我不能在这里像对待任何其他字符一样对待换行符"并运行类似上面的命令.(我知道有人可能会说这是 sed 的语法问题,但请解释一下不允许这样做背后的直觉,以便我摆脱我的困惑.

Why can't I treat "new line character" here just as I would treat any other character and run something like above command. (I understand that one might say that this is a matter of syntax of sed, but could one please explain the intuition behind not allowing this, so that I can get rid of my confusion.

同样,在 vim 编辑器中,我不能使用 :%s/ / /g .为什么会这样?

Similarly, inside the vim editor, I can not use :%s/ / /g . Why so?

我是否需要通过在 sed 和 vim 中使用反斜杠来进一步转义 ?.

Do I need to further escape by using a backslash in sed and from within vim?.

谢谢,

贾格拉提

推荐答案

来自 sed 手册页:

From the sed man page:

通常,sed 将一行输入(不包括其终止换行符)循环复制到模式空间中,(除非在D"函数之后还剩下一些东西),应用所有带有选择该模式的地址的命令空间,将模式空间复制到标准输出,附加换行符,并删除模式空间.

Normally, sed cyclically copies a line of input, not including its terminating newline character, into a pattern space, (unless there is something left after a "D" function), applies all of the commands with addresses that select that pattern space, copies the pattern space to the standard output, appending a newline, and deletes the pattern space.

它在没有换行符的情况下运行,所以你在那里的模式永远无法匹配.您需要做其他事情 - 例如匹配 $(行尾)或 ^(行首).

It's operating on the line without the newline present, so the pattern you have there can't ever match. You need to do something else - like match against $ (end-of-line) or ^ (start-of-line).

这是一个对我有用的例子:

Here's an example of something that worked for me:

$ cat > states
California
Massachusetts
Arizona
$ sed -e 's/$/
> /' states
California

Massachusetts

Arizona

我在 sed 行中的 后面输入了一个文字换行符.

I typed a literal newline character after the in the sed line.

这篇关于什么是换行符——' '的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆