Setting a variable and utilizing sed [duplicate] - bash

This question already has answers here:
Replace one substring for another string in shell script
(16 answers)
Closed 4 years ago.
I've been trying to wrap my head around this for over an hour now and my searches haven't helped yield the answer.
Trying to set a variable inside a bash script. This variable is taking variable-A and removing variable-B from it.
Prefix="$(echo ${Process} | sed -e 's/${Server}//g')"
So if Process=abcd1wxyz01 and Server=wxyz01, then Prefix should end up being abcd1.
I've tried so many iterations from online searches I honestly can't recall what all I've tried.

Your problem are the quotes, as pointed out in afsal_p's answer.
You could do this with parameter expansion instead:
$ process=abcd1wxyz01
$ server=wxyz01
$ prefix=${process%"$server"}
$ echo "$prefix"
abcd1
The ${word%suffix} expansion removes suffix from the end of word.

please use " instead of ' while using bash variables inside sed:
Prefix="$(echo ${Process} | sed -e "s/${Server}//g")"
echo $Prefix

Related

How to replace string-literal that takes the form of shell variable in bash/with sed? [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
sed command not replacing ip address via variables [duplicate]
(1 answer)
Closed 1 year ago.
I'm familiar with sed's ability to replace in-place in a file, as answered here: sed edit file in place.
I.e. sed -i 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' filename
I'm having trouble expanding this concept to replacing a string-literal that takes the form of a shell-variable -- how does one do this?
To illustrate with an example:
Given file file.txt:
# file.txt
set(FOO ${FOO})
...and shell environment variable ${FOO}:
$ FOO=bar
$ echo ${FOO}
bar
...how can I use sed to replace string-literal "${FOO}" in file.txt with the value of shell-variable ${FOO} i.e. "bar"? I.e. I'd like the resulting content of file.txt to be:
# file.txt
set(FOO bar)
I have a mental block thinking past the obviously-incorrect sed -i 's/${FOO}/${FOO}/g' file.txt
I gravitate towards sed because of past experience, and might prefer a sed-centric answer for the same reason. But any solution is probably okay, but with a preference for POSIX-compliance, if shell-native. To be even more specific, this is going to be run in a docker container with a debian-10.3 base...so I suppose solutions that work with any tools included in that distro should be okay as well.

Using sed with ${parameter} [duplicate]

This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 4 years ago.
Problem
How do we use variables in a sed edit string?
Example
The file statement.txt is the sentence
I like my pet bird.
Given a variable ${myPet}, how can we use sed to replace bird with the value in ${myPet}?
What doesn't work
sed -ie 's/bird/${myPet}/g' statement.txt
The result is
I like my pet ${myPet}.
' single quotes don't expand value of a shell variable so you need to use " double quotes here.
myPet="your_value"
sed -ie "s/bird/${myPet}/g" statement.txt

Sed substitution has no effect [duplicate]

This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 7 years ago.
#! /bin/bash
A=$1
B=$2
i= ... # i is a filename generated with find command
j="$(echo $i | sed -E 's/$A/$B/')"
I am trying to change part of i, (A in this case), into something else (B in this case), and store it in j.
I've executed this, but apparently j is identical to i. I'm pretty sure I've put command line arguments correctly.
Did I do something wrong?
You need to replace single quotes with double quotes in 's/$A/$B/'. The following command should work.
j=$( echo $i | sed -E "s/$A/$B/" )

sed. passing a variable in sed command [duplicate]

This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 7 years ago.
I want to use sed command in a loop passing a variable say a such that it searches for a and in the line it gets a it replaces "true" to "false".
I have a text file containing 3000 different names and another xml file containing 15000 lines. in the lines in which these 3000 entries are there i need to make changes.
I have written a code snippet but that is not giving expected output. Can anyone help. Thanks in advance.
for i in {1..3000}; do
a=`awk NR==$i'{print $1}' names.txt`
# echo $a
sed -e '/$\a/ s/true/false/' abc.xml > abc_new.xml
done
You have to replace single-quotes(') around sed's parameters with double-quotes("). In bash, single-quote won't allow variable expansion. Also, you might want to use sed's in-place edit (pass -i option) in your for loop.
So the one liner script will look like:
for a in `cat names.txt`; do sed -i.bak -e "/$a/s/true/false/" abc.xml ; done

How to use a bash script variable with sed [duplicate]

This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 6 years ago.
I execute the following bash script:
#!/bin/bash
version=$1
echo $version
sed 's/\${version.number}/$version/' template.txt > readme.txt
I'm expecting to replace all instances of ${version.number} with the contents of the variable "version". Instead the literal text $version is being inserted.
What do I need to do to make sed use the current value of $version instead?
sed "s/\${version.number}/$version/" template.txt > readme.txt
Only double quotes do dollar-sign replacement. That also means single quotes don't require the dollar sign to be escaped.
You could also simply unquote the variables
sed 's/'${version.number}'/'$version'/' template.txt > readme.txt
It's a bit old, but it might still be helpful...
For me it worked using a double escape as Philip said (and escaping parenthesis, if you use them...):
#!/bin/bash
LIVE_DB_NAME='wordpress';
STAGING='staging';
sed -r "s/define\('DB_NAME', '[a-zA-Z0-9]+'\);/define('DB_NAME', '\\${LIVE_DB_NAME}');/" ${STAGING}/wp-config.php >tmp1;

Resources