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.
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
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
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
This question already has answers here:
Environment variable substitution in sed
(12 answers)
sed substitution with Bash variables
(6 answers)
Closed 10 months ago.
I can't seem to use a variable in a sed command, for example:
sed "24s/.*/"$ct_tname"/" file1.sas > file2.sas
I want $ct_tname the variable, not literally $ct_tname, which is what I keep getting.
Anybody know how to get this to work?
The problem is actually more complex and I omitted some information.
ct_fname="%let outputfile="/user/ct_"$1".csv";"
Here, $1 is the argument passed in at the start of my bash script (sed is being run inside a bash script).
This doesn't run successfully, but it does run if I replace ct_fname with
ct_fname="%let table=ct_$1;"
Is there a way to get the first ct_fname to be passed successfully?
you need to use double quotes (") instead of single quotes (').
single quotes pass their content literally, without translating variables (expansion).
try
sed "24s/.*/\"$ct_tname\"/" file1.sas > file2.sas
btw, if you're going to be editing a file (that is if file2.sas is a temporary file), you should be using ed instead.
In my case, i just remplaced single quotes by the double ones:
for a in $(cat ext.cnf); do sed -n "/$a$/p" file1 >> file2; done
For now, it's working well...
The problem is that when $ct_fname is substituted, sed sees extra / separators, so
sed "24s/.*/"$ct_tname"/" file1.sas > file2.sas
becomes
sed "24s/.*/"%let outputfile=/user/ct_ARGUMENT1.csv;"/" file1.sas > file2.sas
and you'll get a sed error because there are 5 / instead of the expected 3.
Instead, change your sed separators to an unused character like | or :, and either single or double quotes will work just fine:
sed '24s|.*|'$ct_tname'|' file1.sas > file2.sas
sed "24s|.*|"$ct_tname"|" file1.sas > file2.sas
Shell variables are not expanded inside single quotes. Try this instead:
sed "24s/.*/\"$ct_tname\"/" file1.sas > file2.sas
You need to use double (") quotes, with single (') quotes the value of the variable doesn't get replaced. Since you have double quotes in your replacement text, you need to escape them:
sed "24s/.*/\"$ct_tname\"/" file1.sas > file2.sas
Other answers focus on the use of escaped double quotes in their examples. Note that this is not always what you want :
$ FOO="auie"; echo foo123bar|sed "s/123/\"$FOO\"/"
foo"auie"bar
$ FOO="auie"; echo foo123bar|sed "s/123/$FOO/"
fooauiebar
$ FOO="auie"; echo fooauiebar|sed "s/\"$FOO\"/123/"
fooauiebar
$ FOO="auie"; echo fooauiebar|sed "s/$FOO/123/"
foo123bar