Why is SED echoing rather than editing: sed '/^;text1/!b;:a;n;//ba;i\text2' [duplicate] - string

This question already has answers here:
How do I use a new-line replacement in a BSD sed?
(4 answers)
Closed 7 years ago.
sed '/^;date.timezone =/!b;:a;n;//ba;i\date.timezone = Europe/London' /etc/php.ini
You can probably guess Im creating a script for setting up LAMP servers.
In the above example the text is not replaced but instead the changes are displayed on the console.
I my goal was to insert date.timezone = Europe/London the last occurence of ;date.timezone =

Etan Reisner:
You aren't using the -i flag to tell sed to modify in place.

Related

Linux sed command is not working when trying to substitute environment variable [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 4 years ago.
I have an environment variable LIB_HOME=/etc/temp
In one of the config files, i'm trying to replace entire line using sed command as follows
sed -i '/lib.home=/c\lib.home=$LIB_HOME' myconfig.properties
Actual Output : lib.home=blablabla ===> lib.home=$LIB_HOME
Expected Output : lib.home=blablabla ===> lib.home=/etc/temp
Please help
Have you tried using double quotes?
sed -i '/lib.home=/c\lib.home=‘“$LIB_HOME”' myconfig.properties

Replacing text with sed containing whitespace in variable [duplicate]

This question already has answers here:
sed substitution with Bash variables
(6 answers)
how to find replace value with whitespace using sed in a bash script
(2 answers)
Closed 5 years ago.
I try to replace some text within a text file. In my script I have store the text to find and to replace within two variables:
$currentProductName='hello'
$configProductName='byebye'
The replacing is done with this line:
sed -i'.bak' 's/$currentProductName/$configProductName/g' "$projectFile"
Everything works fine until any of my variables are containing whitespaces. If the $configProductName is set to hello world the sedcommand does not work as expected.
I´ve already tried this but it doesn't work, too:
sed -i'.bak' 's/"$currentProductName"/"$configProductName"/g' "$projectFile"
sed -i'.bak' 's/\$currentProductName/\$configProductName/g' "$projectFile"
How do I must change the line to work as expected?
To preserve the spaces, you need to double-quote the variable and wrap it over once again with single quotes. This way you have control over which variables prefixed with $ needs to be expanded.
configProductName='hello world'
Use the sed operation just as below and add the -i flag once you find it working as expected.
sed 's/$currentProductName/'"$configProductName"'/g' file
hello world='hello'
$configProductName='byebye'

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}'

BASH: sed to remove "/" from a $STRING [duplicate]

This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Closed 7 years ago.
First let me apologize for asking a question that:
Has some examples, though I find them confusing
Has a man page, also find confusing
Problem:
I would like to replace text in a $STRING within bash for a script I am writing. I chose to combine date/time to allow for easier end user integration.
STARTTIME="2015-03-17/11:30:00"
sed "Unknown"
Attempted Solution:
sed '/s// /' "$STARTTIME"
Desired result is to remove the "/" and end up with 2015-03-17 11:30:00 to then be passed to a command.
Thank you for any assistance.
If you're using bash, I would suggest that you used built-in string manipulation:
$ s='2015-03-17/11:30:00'
$ echo "${s/\// }"
2015-03-17 11:30:00
The syntax inside the braces means "replace the first occurrence of a forward slash (which needs escaping) with a space".

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