Linux / UNIX: Sed Replace Newline

by Vivek Gite on January 6, 2009 · 2 comments

How do I replace newline (\n) with sed under UNIX / Linux operating systems?

You can use the following sed command:

sed '{:q;N;s/\n//g;t q}' /path/to/data.txt

You can replace newline (\n) with * character or word 'FOO':

sed '{:q;N;s/\n/*/g;t q}' /path/to/data.txt

OR

sed '{:q;N;s/\n/FOO/g;t q}' /path/to/data.txt

OR replace it with tab (\t):

sed '{:q;N;s/\n/\t/g;t q}' /path/to/data.txt

To update file use -i option:

sed -i '{:q;N;s/\n/\t/g;t q}' /path/to/data.txt

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 2 comments… read them below or add one }

1 jalal hajigholamali August 3, 2011

thanks..
very nice example

Reply

2 Claudio October 28, 2011

It doesnt work for me :(

$ sed ‘{:q;N;s/\n/\t/g;t q}’ /fi/fa/foo.fu
sed: 1: “{:q;N;s/\n/\t/g;t q}”: unexpected EOF (pending }’s)

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 6 + 7 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: