Joining all lines in a file using sed
Linux - 1 Comment » - Posted on October, 12 at 10:19 pm
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
: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
Posted in Linux | 1 Comment »
Don’t forget to replace the single quote characters if these ones here don’t match your OS’s type.