Using sed with ${parameter} [duplicate] - bash

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

Related

I can't use sed or jq with variables including whitespaces [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 1 year ago.
I want to add a couple fields to a json file in a shell script. The values for both files come in a parameter and there doesn't seem to be a way to make sed or jq work with such values. I've traid with simple and double quotes everywhere but can't make it work.
sh myscript.sh 'value' 'second value'
firstValue=$1
secondValue=$2
jq '.firstField="'$firstValue'" | .secondField="'$secondValue'"' $jsonFileAddress
sed -i '$s/}/,"firstField":"'$firstValue'","secondField":"'$secondValue'"}/' $jsonFileAddress
Bengamin W. comment put me on track with the use of arguments. I finally managed to append a couple of new fields to an existing json file like this:
echo $(jq --arg a "$firstValue" --arg b "$secondValue" '.firstField=$a | .secondField=$b' $jsonFileAddress) > $jsonFileAddress

Applying sed in bash variable and assigning it to another [duplicate]

This question already has answers here:
Concat numbers from JSON without doublequotes using jq [duplicate]
(1 answer)
How to remove double-quotes in jq output for parsing json files in bash?
(2 answers)
Closed 1 year ago.
I have:
MY_FOLDER=`jq '.results_folder' ./conf.json`
FOLDER_WITHOUT_QUOTES=$MY_FOLDER | sed 's/"//g'
python my_code.py > $FOLDER_WITHOUT_QUOTES/log.log
So, there is a json file with a folder name. But jsons demand strings to be inside ". And reading the json with bash returns me ", which I want to remove
Passing it to a variable and then applying sed isn't working. What's the correct syntax for doing it?
Thank you!
Posix shell would need:
FOLDER_WITHOUT_QUOTES="$(printf '%s\n' "$MY_FOLDER" | sed 's/"//g')"
With Bash you can use the here-document syntax:
FOLDER_WITHOUT_QUOTES=$(sed 's/"//g' <<< "$MY_FOLDER")
... and you can even get rid off a call to sed with the special substitution:
FOLDER_WITHOUT_QUOTES=${MY_FOLDER//\"}
Note: prefer the $(command) syntax to the backquotes which are less readable and cannot be nested as easily.

Sed is taking the file name instead of empty value [duplicate]

This question already has answers here:
Pass empty variable in bash
(3 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 4 years ago.
I have a replace function like
function replace()
{
sed -i "s#$1#$2#g" $3
}
I am calling the function with these parameters
replace MY_IP $MY_IP /usr/xxx.sh
where $MY_IP is a empty value
so sed is giving as sed -i s#MY_IP#/usr/xxx.sh#g
sed no input files
It is not taking the empty value. How to solve this?
Try quoting $MY_IP.
replace MY_IP "$MY_IP" /usr/xxx.sh
Without the quotes, bash will just skip that argument.

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

This question already has answers here:
Use a variable's value in a sed command [duplicate]
(6 answers)
sed substitution with Bash variables
(6 answers)
Closed 4 years ago.
i am just trying to append a line from my bash script:
sed -i '$ a TEXT' "filename"
but when TEXT is a variable it does not work
sed -i '$ a $VARIABLE' "filename"
i have tested endless iterations with ',''," before/after $ and ${}, escape with \ etc., but i always get a literal $VARIABLE in my file
ps. please do not propose to use echo:)

Replace a string containing backslash using sed [duplicate]

This question already has answers here:
Escape a string for a sed replace pattern
(17 answers)
Closed 1 year ago.
I want to substitute a variable SERVICE with a string $service which contains a backslash using sed. I did the following
sed "s/SERVICE/`printf '%q' "${service}"`/g"
Using this I am getting the substituted string as
b_a^c_b_\]Wdd[]X\[X\[W206C?2#,.\\,A#2AW!w6"|
where as I want
b_a^c_b_\]Wdd[]X\[X\[W206C?2#,.\,A#2AW!w6"|
Is there any other way to do it.
PS(The string $service has many different special characters)
You might as well not use sed at all but just bash like this instead:
while read -r; do
echo "${REPLY//SERVICE/$service}"
done

Resources