LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   sed insert newline character (https://www.linuxquestions.org/questions/linux-software-2/sed-insert-newline-character-560269/)

jhwilliams 06-08-2007 01:55 PM

sed insert newline character
 
Is there any way to add a newline character with sed? If not sed, another utility?

I would like to do something like
Code:

echo "word1a word2a " | sed 's|a |\n|'
but the above doesn't actually add a newline.

druuna 06-08-2007 02:03 PM

Hi,

What did you actually try? It does work on my box:

Code:

$ echo "word1a word2a "
word1a word2a
$ echo "word1a word2a " | sed 's|a |\n|'
word1
word2a
$


Marsolin 06-08-2007 02:12 PM

Where do you want the newline? Between the words or somewhere else?

forrestt 06-08-2007 02:20 PM

What is wrong with just doing:
Code:

echo "word1\nword2\n"

jhwilliams 06-08-2007 02:52 PM

Quote:

What did you actually try? It does work on my box:
Fancy that. Yea... sorry, it works on my box too. I guess what I meant to ask was how to get future instances to be replaced too... like the second a. Thanks

druuna 06-08-2007 03:11 PM

Hi,

You make it a global search and replace:

echo "word1a word2a " | sed 's|a |\n|g'

Code:

$ echo "word1a word2a " | sed 's|a |\n|g'
word1
word2a

$

Hope this helps.

forrestt 06-08-2007 03:13 PM

You need a "g" for it to globally replace the string.

Code:

echo 'word1a word2a ' | sed 's|a |\n|g'

Edited: Well, it looks like druuna beat me to it.


All times are GMT -5. The time now is 03:57 PM.