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.
Related
This question already has answers here:
Shell script - search and replace text in multiple files using a list of strings
(5 answers)
Difference between single and double quotes in Bash
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
How do I use shell variables in an awk script?
(7 answers)
Closed 4 months ago.
I tried to find and replace certain strings in text file and the replace text contains "/" and sed commond did not work.
then I tried with awk command and it works fine with terminal with fixed variables.
but when I tried to use it with parameters in the loop it dose not work.
I have attached the code that I have write so far. could you please find me a solution.
this works find in terminal
awk '{sub(/input_image/,"https://www.abcd.com/images/XPDDL_R1_20161007.jpg")}1' format2.svg > format2.html
x=1
for param in ${paramO[#]}
do
awk '{sub(/${param}/,"${paramN[x]}")}1' $output_file > temp.txt
cp -rp temp.txt $output_file
let x=x+1
done
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
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.
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:)
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