What is newline character -- '\n'

128,372

Solution 1

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.

Solution 2

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.

Solution 3

Escape characters are dependent on whatever system is interpreting them. \n is 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 \n as 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).

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.

Solution 4

sed 's/$/\n/' states

Solution 5

I think this post 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).

Share:
128,372
xyz
Author by

xyz

Updated on December 25, 2020

Comments

  • xyz
    xyz over 3 years

    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

  • Michael Mrozek
    Michael Mrozek almost 14 years
    OS 9 uses \r; they dropped it in OS X and switched to matching Unix
  • Adrian McCarthy
    Adrian McCarthy almost 14 years
    That post has the basic jist of the issue, but it also has some factual errors and half-truths. You might be better off reading Wikipedia's newline topic.
  • jamil ahmed
    jamil ahmed almost 14 years
    Right, fixed that. You know, ya'll can edit answers too. :)
  • user353297
    user353297 almost 14 years
    +1. But you've mentioned LF but not actually clarified what it is or even what it stands for. :)
  • jabirali
    jabirali almost 14 years
    \n does work in sed, so you could also use just sed 's/$/\n/' states
  • ssoto
    ssoto about 10 years
    In a file encoded with charset us-ascii, that is not valid. You must to get the ASCII code.
  • Sourav Kannantha B
    Sourav Kannantha B over 2 years
    'windows uses \r\n as newline character'. But "hello, world!\n" prints a new line as expected even in windows!!!!