47

I am new to shell scripting and i am trying to remove new line character from each line using SED. this is what i have done so far :

printf "{new\nto\nlinux}" | sed ':a;N;s/\n/ /g'

removes only Ist new line character. I somewhere found this command :

printf "{new\nto\nlinux}" | sed ':a;N;$!ba;s/\n/ /g'

but it gives :"ba: Event not found."

if i do:

printf "{new\nto\nlinux}" | sed ':a;N;s/\n/ /g' | sed ':a;N;s/\n/ /g'

then it gives correct output but i am looking for something better as i am not sure how many new character i will get when i run the script. incoming stream is from echo or printf or some variable in script.

starball
  • 20,030
  • 7
  • 43
  • 238
nav_jan
  • 2,473
  • 4
  • 24
  • 42
  • The 'ba:Event not found' error is from your shell. Stop using csh, or escape the !. – William Pursell May 16 '12 at 12:50
  • @WilliamPursell i have to use csh only no other options for me.on your suggestion i dropped ! "printf "{new\nto\nlinux}" | sed ':a;N;$ba;s/\n/ /g'" this command too is not giving the correct result .. i know there are many disadvantage of using csh but have to use it due to some compulsion :( ..thanks – nav_jan May 16 '12 at 12:54
  • You cannot remove the '!', but you need to precede it with a backslash. – William Pursell May 16 '12 at 12:55
  • The "found somewhere" might well have been http://stackoverflow.com/a/1252191/512360 - see there for an explanation as to how it works. – FrankH. Dec 14 '12 at 13:44

3 Answers3

106

To remove newlines, use tr:

tr -d '\n'

If you want to replace each newline with a single space:

tr '\n' ' '

The error ba: Event not found is coming from csh, and is due to csh trying to match !ba in your history list. You can escape the ! and write the command:

sed ':a;N;$\!ba;s/\n/ /g'  # Suitable for csh only!!

but sed is the wrong tool for this, and you would be better off using a shell that handles quoted strings more reasonably. That is, stop using csh and start using bash.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • thanks for your reply but you can we use tr with to modify incoming stream from echo or printf?? – nav_jan May 16 '12 at 12:48
  • Nope, this still does not work. Even with the `$\!ba`, the result is `ba: Event not found` from a csh script. – krb686 May 21 '15 at 14:11
  • The answer should be `sed ':a;N;$\\!ba;s/\n/ /g'` – krb686 May 21 '15 at 14:12
  • I wanted to replace some but not all newlines in a file. e.g. all lines ending with PULLUP. This worked: `sed ':a;N;$!ba;s/PULLUP\n/ /g;'`. – gaoithe Sep 10 '15 at 11:32
7

This might work for you:

printf "{new\nto\nlinux}" | paste -sd' '            
{new to linux}

or:

printf "{new\nto\nlinux}" | tr '\n' ' '            
{new to linux}

or:

printf "{new\nto\nlinux}" |sed -e ':a' -e '$!{' -e 'N' -e 'ba' -e '}' -e 's/\n/ /g'
{new to linux}
potong
  • 55,640
  • 6
  • 51
  • 83
0

Use perl instead of sed. perl is similar to sed:

ubuntu@ubuntu:/$ printf "{new\nto\nlinux}" | sed 's/\n/ /g'; echo ''
{new
to
linux}
ubuntu@ubuntu:/$ printf "{new\nto\nlinux}" | perl -pe 's/\n/ /g'; echo ''
{new to linux}
ubuntu@ubuntu:/$ echo -e "new\nto\nlinux\ntest\n1\n2 3" | perl -pe 's/\n/_ _/g'; echo ''
new_ _to_ _linux_ _test_ _1_ _2 3_ _
ubuntu@ubuntu:/$
Rublacava
  • 414
  • 1
  • 4
  • 16