Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: How can I replace a string by new line in osx bash?

Tags:

bash

sed

I am googling it a lot. I only want that this line:

echo "Replace <newLine> it by <newLine> NEWLINE <newLine> in my OSX terminal <newLine> and bash script" | sed -e 's/<newLine>/\n/g'

works in my osx terminal and in my bash script. I can't use sed for this? Is there another one line solution?

like image 246
Rodrigo Avatar asked May 07 '12 20:05

Rodrigo


People also ask

How do I replace a string with a new line?

In the "Replace" dialog, make sure that under Search Mode you have "Extended" selected. Then type in the find box enter "xyz" and replace with "\n".

How do I replace a string in a bash script?

The 'sed' command is used to replace any string in a file using a bash script. This command can be used in various ways to replace the content of a file in bash. The 'awk' command can also be used to replace the string in a file.

How do I add a new line in bash?

use ctrl-v ctrl-m key combos twice to insert two newline control character in the terminal. Ctrl-v lets you insert control characters into the terminal. You could use the enter or return key instead of the ctrol-m if you like. It inserts the same thing.


3 Answers

Here is using sed

echo "Replace <newLine> it by <newLine> NEWLINE <newLine> in my OSX terminal <newLine> and bash script" | sed 's/<newLine>/\'$'\n/g'

And here is a blogpost explaining why - https://nlfiedler.github.io/2010/12/05/newlines-in-sed-on-mac.html

like image 87
xeor Avatar answered Oct 27 '22 10:10

xeor


Using bash only:

STR="Replace <newLine> it by <newLine> NEWLINE <newLine> in my OSX terminal <newLine> and bash script"
$ echo ${STR//<newLine>/\\n}
Replace \n it by \n NEWLINE \n in my OSX terminal \n and bash script

$ echo -e ${STR//<newLine>/\\n}
Replace 
 it by 
 NEWLINE 
 in my OSX terminal 
 and bash script

A quick explanation here - the syntax is similar to sed's replacement syntax, but you use a double slash (//) to indicate replacing all instances of the string. Otherwise, only the first occurrence of the string is replaced.

like image 39
spinlok Avatar answered Oct 27 '22 11:10

spinlok


This might work for you:

echo "Replace <newLine> it by <newLine> NEWLINE <newLine> in my OSX terminal <newLine> and bash script" |
sed 'G;:a;s/<newLine>\(.*\(.\)\)$/\2\1/;ta;s/.$//' 
Replace 
 it by 
 NEWLINE 
 in my OSX terminal 
 and bash script

EDIT: OSX doesn't accept multiple commands see here

echo "Replace <newLine> it by <newLine> NEWLINE <newLine> in my OSX terminal <newLine> and bash script" | 
sed -e 'G' -e ':a' -e 's/<newLine>\(.*\(.\)\)$/\2\1/' -e 'ta' -e 's/.$//' 
Replace 
 it by 
 NEWLINE 
 in my OSX terminal 
 and bash script

Yet another way:

echo "Replace <newLine> it by <newLine> NEWLINE <newLine> in my OSX terminal <newLine> and bash script" |
sed $'s|<newLine>|\\\n|g' 
Replace 
 it by 
 NEWLINE 
 in my OSX terminal 
 and bash script
like image 38
potong Avatar answered Oct 27 '22 09:10

potong