Adding Newlines with sed

By | July 3, 2008

Not realizing there was a surfeit of newline characters in a largish mysqldump file I was analyzing, I tried to open it in TextMate. After a few minutes of listening to the hard drive in my Mac thrash away, I had to kill TextMate. One obvious solution was to split it into multiple lines, but obviously I couldn’t use TextMate for that.

Fortunately, the Unix utility sed is a great tool for problems like this. My file had the character string “\n” between all the parts that were reasonable to split into separate lines. The following command did the trick:

$ cat source.txt | sed 's:\\n:\
:g' > dest.txt

First, I used the cat utility to pipe the contents of source.txt into sed. Then, I used the substitute command to replace \n (the extra backslash is needed to escape the special treatment of backslashes) with a carriage return. The \ at the end of the first line escapes the literal new line character that causes the shell to go to the next line. The g tells sed to make this substitute globally throughout the file. I then redirected the output into dest.txt.

Sweet, sweet, Unix.

2 thoughts on “Adding Newlines with sed

  1. JohnB

    Huh. This is one of those cases where the right tool wins. Ruby is a bit behind sed for this task:

    ruby -ne ‘puts $_.gsub(“\\n”,”\n”)’ source.txt > dest.txt
    or
    ruby -e ‘puts IO.read(“source.txt”).gsub(“\\n”,”\n”)’ > output.txt

    Reply
  2. Diederick

    Never new that in sed a return key press in fact is a newline. Thanks!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.