C语言 什么是换行符——'\n'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3267311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:54:40  来源:igfitidea点击:

What is newline character -- '\n'

cunixvimsednewline

提问by xyz

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.

这是一个非常基本的概念,但我从来没有能够很好地表达出来。我想尝试拼写它,看看我哪里出错了。

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 '\n' . And why this confusing 2 characters to represent end of line characters..

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

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/\n/\n\n/g' states  does not work.

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.

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

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

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

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

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

Thanks,

谢谢,

Jagrati

贾格拉蒂

回答by i_am_jorf

NewLine (\n) is 10 (0xA) and CarriageReturn (\r) is 13 (0xD).

NewLine (\n) 是 10 (0xA),CarriageReturn (\r) 是 13 (0xD)。

Different operating systems picked different end of line representations for files. Windows uses CRLF (\r\n). Unix uses LF (\n). Older Mac OS versions use CR (\r), but OS X switched to the Unix character.

不同的操作系统为文件选择了不同的行尾表示。Windows 使用 CRLF (\r\n)。Unix 使用 LF (\n)。较旧的 Mac OS 版本使用 CR (\r),但 OS X 切换到 Unix 字符。

Here is a relatively useful FAQ.

这里有一个比较有用的FAQ

回答by Carl Norum

From the sedman page:

sed手册页

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.

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

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).

它在没有换行符的情况下在行上运行,因此您在那里拥有的模式永远无法匹配。你需要做一些其他的事情——比如匹配$(end-of-line) 或^(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

I typed a literal newline character after the \in the sedline.

我后输入文字换行字符\sed行。

回答by Cogwheel

Escape characters are dependent on whatever system is interpreting them. \nis interpreted as a newline character by many programming languages, but that doesn't necessarily hold true for the other utilities you mention. Even if they do treat \nas newline, there may be some other techniques to get them to behave how you want. You would have to consult their documentation (or see other answers here).

转义字符取决于解释它们的任何系统。\n许多编程语言将其解释为换行符,但这不一定适用于您提到的其他实用程序。即使他们确实将其\n视为换行符,也可能有一些其他技术可以让他们按照您的意愿行事。您将不得不查阅他们的文档(或在此处查看其他答案)。

For DOS/Windows systems, the newline is actually two characters: Carriage Return (ASCII 13, AKA \r), followed by Line Feed (ASCII 10). On Unix systems (including Mac OSX) it's just Line Feed. On older Macs it was a single Carriage Return.

对于 DOS/Windows 系统,换行符实际上是两个字符:回车(ASCII 13,AKA \r),然后是换行符(ASCII 10)。在 Unix 系统(包括 Mac OSX)上,它只是换行。在较旧的 Mac 上,它是单个回车。

回答by Johan

sed 's/$/\n/' states

回答by Corina

I think thispost by Jeff Attwood addresses your question perfectly. It takes you through the differences between newlines on Dos, Mac and Unix, and then explains the history of CR (Carriage return) and LF (Line feed).

我认为Jeff Attwood 的这篇文章完美地解决了您的问题。它带您了解 Dos、Mac 和 Unix 上换行符之间的差异,然后解释 CR(回车)和 LF(换行)的历史。

回答by cahn

sedcan be put into multi-line search & replace mode to match newline characters \n.

sed可以进入多行搜索和替换模式以匹配换行符\n

To do so sedfirst has to read the entire file or string into the hold buffer ("hold space") so that it then can treat the file or string contents as a single line in "pattern space".

为此,sed首先必须将整个文件或字符串读入保持缓冲区(“保持空间”),以便它可以将文件或字符串内容视为“模式空间”中的一行。

To replace a single newline portably (with respect to GNU and FreeBSD sed) you can use an escaped "real" newline.

要可移植地替换单个换行符(相对于 GNU 和 FreeBSD sed),您可以使用转义的“真实”换行符。

# cf. http://austinmatzko.com/2008/04/26/sed-multi-line-search-and-replace/
echo 'California
Massachusetts
Arizona' | 
sed -n -e '
# if the first line copy the pattern to the hold buffer
1h
# if not the first line then append the pattern to the hold buffer
1!H
# if the last line then ...
$ {
# copy from the hold to the pattern buffer
g
# double newlines
s/\n/\
\
/g
s/$/\
/
p
}'

# output
# California
#
# Massachusetts
#
# Arizona
#

There is, however, a much more convenient was to achieve the same result:

然而,有一个更方便的方法是实现相同的结果:

echo 'California
Massachusetts
Arizona' | 
   sed G

回答by benjifisher

I see a lot of sed answers, but none for vim. To be fair, vim's treatment of newline characters is a little confusing. Search for \nbut replace with \r. I recommend RTFM: :help patternin general and :help NL-used-for-Nulin particular.

我看到很多 sed 答案,但没有一个适用于 vim。公平地说,vim 对换行符的处理有点令人困惑。搜索\n但替换为\r。我推荐 RTFM: :help pattern一般来说:help NL-used-for-Nul,特别是。

To do what you want with a :substitute command,

用 :substitute 命令做你想做的事,

:%s/\_$/\r

although I think most people would use something like

虽然我认为大多数人会使用类似的东西

:g/^/put=''

for the same effect.

为了同样的效果。

Here is a way to find the answer for yourself. Run your file through xxd, which is part of the standard vim distribution.

这是一种为自己找到答案的方法。通过 xxd 运行您的文件,它是标准 vim 发行版的一部分。

:%!xxd

You get

你得到

0000000: 4361 6c69 666f 726e 6961 0a4d 6173 7361  California.Massa
0000010: 6368 7573 6574 7473 0a41 7269 7a6f 6e61  chusetts.Arizona
0000020: 0a                                       .

This shows that 46 is the hex code for C, 61 is the code for a, and so on. In particular, 0a (decimal 10) is the code for \n. Just for kicks, try

这表明 46 是 的十六进制代码C,61 是 的代码a,依此类推。特别是,0a(十进制 10)是 的代码\n。只是为了踢,试试

:set ff=dos

before filtering through xxd. You will see 0d0a (CRLF) as the line terminator.

在通过 xxd 过滤之前。您将看到 0d0a (CRLF) 作为行终止符。

:help /\_$
:help :g
:help :put
:help :!
:help 23.4

回答by Adrian

Try this:

尝试这个:

$ sed -e $'s/\n/\n\n/g' states