bio photo

Twitter Github

Sed (Stream EDitor) is a very powerful stream editor to manipulate text files. I needed an utility that would join all lines in a text file to a single line with “\n” as a separator so that I could easily cut and paste inline velocity templates into java code. Here is the final sed command !

sed -e :a -e ‘/$/N; s/\n/\\n/; ta’ [filename]

Explanation :

-e - denotes a command to be executed
:a - is a label
/$/N - defines the scope of the match for the current and the (N)ext line
s/\n/\\n/; - replaces all EOL with “\n”
ta; - goto label a if the match is sucessful