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?
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".
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.
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.
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With