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'
Related
This question already has answers here:
How to replace a value with the output of a command in a text file?
(2 answers)
Closed 5 years ago.
my sed input is as follows:
sed 's/ListenAddress=.*/ListenAddress= $hostname/' nodemanager.properties
I am trying to run this against a Linux server and replace ListenAddress={current_value} with ListenAddress={hostname_of_server}
I need to know how to run the hostname command and have that output be reflected at the end of ListenAddress=
Thanks
If you wish your bash variables to reflect inside the sed script use, double quotes. The same is valid for command substitution. So you should be doing
sed "s/ListenAddress=.*/ListenAddress= $(hostname)/" nodemanager.properties
Since thevariable expansion takes place, you need to be careful about certain situations where $ appear as a sed attribute. For example if you're applying the above command only to the last line of the file, then do
sed "\$s/ListenAddress=.*/ListenAddress= $(hostname)/" nodemanager.properties
Note the $ before s command is escaped meaning that it is literal $ supplied to the script.
This question already has answers here:
How to escape single quote in sed?
(9 answers)
Closed 6 years ago.
I have a config.js file which contents below strings
.constant('Digin_Engine_API', 'http://local.net:1929/')
I want to read this file and replace what ever the things which are there after the .constant('Digin_Engine_API'I tried using sedbut ddnt worked. This is what I used for sed
sed -i 's/^.constant('Digin_Engine_API', .*/http://cloud.lk:8080/' /var/config.js
As a summary my final out put (config.js) file needs to consists below.
Before
.constant('Digin_Engine_API', 'http://local.net:1929/')
After
.constant('Digin_Engine_API', 'http://cloud.lk:8080/')
You need to use double quotes around sed command since single quote is part of pattern
You should use an alternate delimiter since / is used in replacement
You need to capture the first part and use it in replacement
You need to quote the replacement and also add closing )
Sed command:
sed -i.bak "s~\(\.constant('Digin_Engine_API', \).*~\1'http://cloud.lk:8080')~" /var/config.js
cat /var/config.js
.constant('Digin_Engine_API', 'http://cloud.lk:8080')
Here you are:
sed -i -r "s_(\.constant\('Digin_Engine_API').*_\1, <new content>)_" file
Remarks
you cannot use ' in sed command if command is surrounded by '' also,
you must escape all ( and ) that are part or string, and not the sed grouping command,
you must escape . character, because it is also sed replacement for every char,
you must use another sed s separator instead of / if you need to use / in that command, but you can also escape / by \/.
This question already has answers here:
How to insert a line using sed before a pattern and after a line number?
(5 answers)
Closed 6 years ago.
In a shell script, I get the certain number of a line in a file and store it in a variable using the code below:
res=$(grep -n '123' somefile.txt | sed 's/^\([0-9]\+\):.*$/\1/')
Now I want to write in 3 lines after this line, so I used these codes :
sed -i '$res\anything' somefile.txt
sed -i '$resianything' somefile.txt
but they don't work. It seems that sed doesn't accept variables.
How can I solve it? Is there any alternative way?
Some tips before pointing out the duplication:
Variables won't be expanded within single quotes.
Variable name boundaries are not detected automatically. $resianything will not expand to the value of the variable res followed by the string ianything, but rather to the value of the variable with the name resianything, which presumably is empty. You can detect this kind of error by adding set -o nounset (and ideally -o errexit) at the top of your script.
You'll definitely want to escape the string you're inserting back into a sed command.
You seem to be asking why the $res isn't expanding. That isn't a sed issue; it's the shell. Typically bash &c. don't expand variables within single quotes. Try double quoting that bit.
Also, one method for separating your variable from following alphanumeric text is to use curly braces. Try ${res}anything instead of $resanything, for example.
This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 8 years ago.
I have an xml file with line below
<config-root>/temp</config-root>
Using 'sed' is bash shell script I want to replace the line,
the 'sed' script is below
sed -i 's/<config-root>\(.*\)<\/config-root>/<config-root>\"${dirPath}"<\/config-root>/' Plan.xml
The 'sed' is resulting in
<config-root>"${dirPath}"</config-root>
I am expecting the line to be replaced as /opt/shared
Can anyone let me know what is wrong in my script? Basically I want to use variable in ‘sed’
Thanks in advance,
Babu
You can use bash to place the variable in the sed script: End the sed script using the single quote ', place the variable in double quotes " and continue the sed program with another single quote ':
sed 's~<config-root>[^<]*</config-root>~<config-root>'"$variable"'</config-root>~' Plan.xml
I would encourage you to use delimiter different from / because the / is part of the pattern (and of the variable) and would need to get escaped otherwise. I used ~ as the delimiter.
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.