Issue
I have a data in below format.
new_name='abc &eft / def \ mno'
1.json
{
"text": {
"attribute": "old_name",
"data": "xyz"
}
}
I am trying to replace the value old_name by new_name.
My trail :--
sed "s/old_name/${new_name/&/\&}; s/old_name/${new_name////\/}/g" 1.json
I get below error :--
sed: -e expression #1, char 67: unknown option to `s'
Any guidance would be of great help.
Solution
You can skip the need to escape the slash by using a different delimiter for the substitution, but you still need to do one of the other replacements outside the sed command:
new_name='abc &eft / def \ mno'
quoted=${new_name/\\/\\\\\\\\}
sed "s=old_name=${quoted//&/\\&}=g" 1.json
But jq
is far better tool to handle json:
new_name='abc &eft / def \ mno'
jq --arg n "$new_name" \
'.text.attribute |= (if . == "old_name" then $n else . end)' 1.json
Answered By - choroba Answer Checked By - Mary Flores (WPSolving Volunteer)