How to stop sed from inserting newline? [duplicate] - linux

This question already has answers here:
Using sed's append/change/insert without a newline
(2 answers)
"echo -n" prints "-n"
(11 answers)
Closed 4 years ago.
I want to stop sed from adding a newline with the i editing command:
>echo "hello" | sed 'i TEST'
TEST
hello
The result I want is:
TESThello

I think it is not possible. Do it this way instead:
sed 's/^/TEST/' file

You can try this too:
echo "hello" | sed "s/$/TEST/"
What it is doing is replacing the end of the line (/$/) with the text you want.

Related

SED replace a word inside a bracket [duplicate]

This question already has answers here:
What is the difference between `sed -i -e` and `sed -ie`?
(2 answers)
Closed 2 years ago.
I need to change some words in bulk, but because of the brackets inside it, I think I do something wrong.
Line that needs to be changed
echo "CMD_PLUGINS_ADMIN/admin/index.html";
I need to change it to this:
echo "CMD_PLUGINS_ADMIN/reseller/index.html";
I tried it with: sed -ie 's/admin/reseller/' *
But does not change anything, I hope someone knows the right command for it.
$ echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's/\/admin\//\/reseller\//g'
"CMD_PLUGINS_ADMIN/reseller/index.html";
your input has slash and you are using slash as sed seperator
Either escape the slashes in input by preceeding them with backslash:
echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's/\/admin\//\/reseller\//g'
"CMD_PLUGINS_ADMIN/reseller/index.html";
OR change seperator to any other like pipe:
echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's|admin|reseller|g'
"CMD_PLUGINS_ADMIN/reseller/index.html";

Print new line as string literal in unix or shell [duplicate]

This question already has answers here:
How to replace one character with two characters using tr
(5 answers)
Closed 3 years ago.
Hi I have a shell script that has
variable="apple banana monkey"
I want it to be
apple\nbanana\nmonkey
But when I try and execute
echo $variable | tr ' ' '\n'
It results to
apple
banana
monkey
I want to get the actual literal of new line and not the evaluated value.
I have tried echo -e or echo -n or even put numerous escapes \\ but to no avail.
Please help. Thanks
tr command translates chars into chars by performing a 1 to 1 mapping. You are asking the tool to translate a space into two chars, which is something that cannot be done with tr.
If you accept a command switch, you can try with sed:
echo "$variable" | sed 's/ /\\n/g'

How to delete last found value in Bash [duplicate]

This question already has answers here:
Bash : extracting part of a string
(4 answers)
Closed 6 years ago.
Say I have a string 0.0.25, how do I delete the last part after dot (including it) to make it like 0.0? Note that last part can have variable number of digits.
In bash you can do the following:
echo "${var%.*}"
See the Shell Parameter Expansion section of the manual.
Using awk you could:
echo "0.0.25" | awk -F. '{print $1"."$2}'

Replacing word in bash script [duplicate]

This question already has answers here:
How to replace to apostrophe ' inside a file using SED
(4 answers)
Closed 7 years ago.
I'm trying to replace two words with one in a file.
sed 's/will not/ain't/g' codes_word
The above doesn't work. What am I doing wrong?
replace ' with "
output
shEll$ echo "Hello World!" | sed "s/World/ain't/g"
Hello ain't!

Replace a text with a variable [duplicate]

This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 7 years ago.
How can I do this?
sed -i 's/wiki_host/$host_name/g' /root/bin/sync
It will replace wiki_host with the text $host_name.
But I want to replace it with the content of the variable..
I tried it with
sed -i 's/wiki_host/${host_name}/g' /root/bin/sync
It doesn't work either.
You need to use double quotes:
$ sed -i "s/wiki_host/${host_name}/g" /root/bin/sync
Your single quotes prevent the shell variable from being replaced with its contents.

Resources