28

Here are my attempts to replace a b character with a newline using sed while running bash

$> echo 'abc' | sed 's/b/\n/'
anc

no, that's not it

$> echo 'abc' | sed 's/b/\\n/'
a\nc

no, that's not it either. The output I want is

a
c

HELP!

4
  • 1
    This works for me the way you want it on Ubuntu 11.10 with GNU sed. What version of sed are you using?
    – Dan Fego
    Jan 24, 2012 at 17:30
  • echo 'abc' | sed 's/b/\n/' works fine on my bash on Debian (Wheezy). My bash version: GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu). My sed version: GNU sed version 4.2.1
    – Susam Pal
    Jan 24, 2012 at 17:31
  • Not sure, manpage doesn't say and sed -V doesn't work, but I'm on SunOS 5.10
    – spraff
    Jan 24, 2012 at 17:33
  • 2
    On Solaris, you can get the POSIX compatible 'sed' by setting this path: PATH=/usr/xpg6/bin:/usr/xpg4/bin:/usr/css/bin:$PATH
    – Susam Pal
    Jan 24, 2012 at 17:40

5 Answers 5

18

Looks like you are on BSD or Solaris. Try this:

[jaypal:~/Temp] echo 'abc' | sed 's/b/\ 
> /'
a
c

Add a black slash and hit enter and complete your sed statement.

14
  • Yes! But why do I need the \ before I hit enter?
    – spraff
    Jan 24, 2012 at 17:34
  • 1
    Once I realized he was on Solaris I tried the manual-Enter, but forgot the backslash at the end of the line. Good catch.
    – Dan Fego
    Jan 24, 2012 at 17:35
  • @spraff Solaris sed is extremely dated, and requires an explicit newline instead of a \n. Jan 24, 2012 at 17:36
  • 7
    If you have bash then you should also be able to write it like this, even on Solaris, and have it work: echo abc | sed 's/b/\'$'\n'/ - the advantage here is that you don't have that pesky significant newline, which makes copying and pasting easier. $'\n' is a bashy way to insert literal escape sequences. By the time sed sees it it's the same as Jaypal's version.
    – sorpigal
    Jan 24, 2012 at 17:47
  • 1
    Also: In this case the backslash is NOT being interpreted by bash, but rather by sed. sed requires that newlines in the replacement pattern be escaped with \. Consider this: (set -x ; echo abc | sed "s/b/\$PATH/") which shows that bash has interpreted \$ and sed sees only $, vs this: (set -x ; echo abc | sed 's/b/\'$'\n'/) which shows that bash has expanded $'\n' but left the \ before it to sed. For comparison try running echo abc | sed "s/b/\"$'\n'/ and see what you get.
    – sorpigal
    Jan 24, 2012 at 17:55
7
$ echo 'abc' | sed 's/b/\'$'\n''/'
a
c

In Bash, $'\n' expands to a single quoted newline character (see "QUOTING" section of man bash). The three strings are concatenated before being passed into sed as an argument. Sed requires that the newline character be escaped, hence the first backslash in the code I pasted.

1
  • 2
    Nice answer! echo 'abc' | sed $'s,b,\\\n,' is even clearer, I think. Feb 10, 2017 at 23:46
5

You didn't say you want to globally replace all b. If yes, you want tr instead:

$ echo abcbd | tr b $'\n'
a
c
d

Works for me on Solaris 5.8 and bash 2.03

1

In a multiline file I had to pipe through tr on both sides of sed, like so:

echo "$FILE_CONTENTS" | \ tr '\n' ¥ | tr ' ' ∑ | mySedFunction $1 | tr ¥ '\n' | tr ∑ ' '

See unix likes to strip out newlines and extra leading spaces and all sorts of things, because I guess that seemed like the thing to do at the time when it was made back in the 1900s. Anyway, this method I show above solves the problem 100%. Wish I would have seen someone post this somewhere because it would have saved me about three hours of my life.

-1
echo 'abc' | sed 's/b/\'\n'/' 

you are missing '' around \n

3
  • just tested on ubuntu 11.01 and it works. my bash version is GNU bash, version 4.2.10(1)-release (i686-pc-linux-gnu) and sed version is 4.2.1
    – Ravi Bhatt
    Jan 24, 2012 at 17:32
  • Works for me on bash 4.3.42(1) May 5, 2016 at 2:00
  • this is just another way of writing the first command in the question and it will fail on BSD-ish and Solaris versions of sed in exactly the same way
    – jhnc
    Nov 15 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.