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.
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
Never new that in sed a return key press in fact is a newline. Thanks!