Pregunta

How can I substitute a character or expression for a newline using the Unix sed command?

(For character substitution, I'd normally use tr ';' "\n" command.)

¿Fue útil?

Solución

  1. Substitute ; character (semicolon) for a newline using Sed:

    sed "s/;/\\
    /g"
    

    or

    sed "s/;/\\$(echo -e '\n')/g"
    
  2. Substitute [^A-Za-z] expression (anything but alphabetic characters) for a newline using Sed:

    sed "s/[^A-Za-z]/\\
    /g"
    

    or

    sed "s/[^A-Za-z]/\\$(echo -e '\n')/g"
    

Otros consejos

a backslash before a literal newline is the portable way to get a newline in sed:

$ echo "a,b" | sed 's/,/\
/'
a
b

How about:

sed $'s/;/\\\n/g' inputFile

$ cat inputFile
this;is;a;text
$ sed $'s/;/\\\n/g' inputFile
this
is
a
text

This might work for you (GNU sed & Bash):

sed 'y/;/\n/' file

or

sed $'y/;/\\n/' file

or very convolutedly:

sed -e 'G' -e ':a' -e 's/\(;\)\(.*\(.\)\)/\3\2/' -e 'ta' -e 's/.$//' file
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top