Skip to content

Instantly share code, notes, and snippets.

@YumaInaura
Last active August 23, 2018 00:32
Show Gist options
  • Save YumaInaura/0763a99557f66634ae6ca0c9f27cc464 to your computer and use it in GitHub Desktop.
Save YumaInaura/0763a99557f66634ae6ca0c9f27cc464 to your computer and use it in GitHub Desktop.
Shell — How to replace "to" newline with sed command ( BSD )

Shell — How to replace "to" newline with sed command ( BSD )

Answer

Use GNU.

GNU sed

echo 'A:B:C' | sed 's/:/\n/g'
A
B
C

No wonder. understandable.


  • sed (GNU sed) 4.4
  • docker run --interactive --tty ubuntu /bin/bash

BSD sed

echo 'A:B:C' | sed 's/:/\
/g'

A
B
C

or

echo 'A:B:C' | sed -e 's/:/\'$'\n/g'
A
B
C

Are you really?

I don’t know what does mean '$' and don’t wanna understand.


  • Mac OS X High Sierra
  • GNU bash, version 4.4.0(1)-release (x86_64-apple-darwin15.6.0)

troubling examples with BSD

All examples does not work well as I expected.

echo 'A:B:C' | sed -e 's/:/\n/g'
AnBnC
echo 'A:B:C' | sed 's/:/\n/g'
AnBnC
echo 'A:B:C' | sed 's/:/\\n/g'
A\nB\nC
echo 'A:B:C' | sed 's/:/\r/g'
ArBrC
echo 'A:B:C' | sed 's/:/\\r/g'
A\rB\rC
echo 'A:B:C' | sed "s/:/\n/g"
AnBnC
echo 'A:B:C' | sed "s/:/\\n/g"
AnBnC
echo 'A:B:C' | sed "s/:/\\r/g"
ArBrC
echo 'A:B:C' | sed "s/:/\r/g"
ArBrC
echo 'A:B:C' | sed "s/:/\r\n/g"
ArnBrnC
echo 'A:B:C' | sed "s/:/\\r\\n/g"
ArnBrnC
echo 'A:B:C' | sed "s/:/\\\r\\\n/g"
A\r\nB\r\nC

Ref

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment