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!
Related
This question already has answers here:
Extract substring in Bash
(26 answers)
Closed 6 years ago.
How to retrieve the first 10 characters of a variable with Bash?
FOO="qwertzuiopasdfghjklyxcvbnm"
I need to get qwertzuiop.
If the variable is: FOO="qwertzuiopasdfghjklyxcvbnm"
then
echo ${FOO:0:10}
will give the first 10 characters.
Use the head command.
echo $FOO | head -c 10
=> qwertzuiop
This question already has answers here:
How to find/replace and increment a matched number with sed/awk?
(5 answers)
Closed 4 years ago.
Suppose I have the following Bash script:
#!/bin/bash
INCREMENT_BY=5
sed 's/20000/&+$INCREMENT_BY/g' old > new
I expect all occurrences of 20000 to be replaced by 20005, but instead they are replaced with 20000+$INCREMENT_BY. How can I make this work?
You should use double quote for eval variable, like:
sed "s/20000/$(( $INCREMENT_BY + 2000))/g" old
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.
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}'
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.