Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is newline character -- '\n'

Tags:

c

vim

unix

newline

sed

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

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.

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

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

Thanks,

Jagrati

like image 993
xyz Avatar asked Jul 16 '10 17:07

xyz


People also ask

What is a newline character \n?

The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.

Is \n and \r same?

\n is used for the newline or linefeed, whereas \r is the carriage return.

What is a newline character in Python?

In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line. Essentially the occurrence of the “\n” indicates that the line ends here and the remaining characters would be displayed in a new line.

Is newline a character in C?

In programming languages, such as C, Java, and Perl, the newline character is represented as a '\n' which is an escape sequence.


2 Answers

NewLine (\n) is 10 (0xA) and CarriageReturn (\r) is 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.

Here is a relatively useful FAQ.

like image 77
i_am_jorf Avatar answered Nov 15 '22 20:11

i_am_jorf


From the sed man page:

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

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

like image 29
Carl Norum Avatar answered Nov 15 '22 19:11

Carl Norum