bash: initiate command inside of a string. - using sed [duplicate] - linux

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.

Related

How to use sed to replace text with a file path? [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Closed 2 years ago.
I'm writing a bash script where I need to replace text in a file with a specific file path, but my understanding is that sed does not work with specific characters such as /. I'm wondering if there is some way around this?
Here is my script currently:
currentdir="$PWD"
filepathvar="${currentdir}/settings.ini"
sed -i -e "s/filepath/$filepathvar/g" aimparmstest
When I print out filepathvar everything is as I expect it to be, but it seems the fact that filepathvar contains special characters, it gives me the following error:
sed: -e expression #1, char 13: unknown option to `s'
Is there any way around this? Or perhaps another command I can use? I haven't had any success with changing around the parameters. Any help is greatly appreciated.
You can use any character as the separator (the first character). For example:
echo "a/b/c" | sed -e 's|/|_|g'
In your case:
sed -i -e "s|filepath|$filepathvar|g" aimparmstest

How to source a key value paired file in bash escaping whitespace? [duplicate]

This question already has answers here:
Use key/value data from a file in a shell script
(1 answer)
Reading key/value parameters from a file into a shell script
(1 answer)
Closed 3 years ago.
$ cat foo.txt
a=1one
b=2two
c=3 three
d=4four
$ source foo.txt
bash: three: command not found...
Need to set all the variable listed in foo.txt, how to source this file by escaping the space character? foo.txt comes from other application, which I cannot control, or is there an alternative to source ?
If the output is so regular, you could try to preprocess the file using sed like this:
$ sed -e "s/=/='/;s/$/'/" < foo.txt >sourced.env
and then source sourced.env. This will add a ' just after the = and add an ending '.

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'

Write into a certain line number of a file using a variable [duplicate]

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.

how to use variables in SED [duplicate]

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.

Resources