Issue
I am working on a bash script, trying to find a regexp in a file, substitute (with sed) a part of regexp corresponding to a float number by this number divided by 2.
Example:
echo set NEX \"2.00\" > testfile
cat testfile
Output:
set NEX "2.00"
Command:
sed -i -r 's/(set NEX ")([0-9.]+)(")/echo \1$(echo "scale=2;\2\/2" | bc)\3/e' testfile
cat testfile
Output:
set NEX 1.00
So, my problem is that echo in sed command seems not to escape and interpret the double quote returned by \1 and \3. I tried many escaping tricks like using \ or '"'"' but it still does not work. I will be happy to have some help and to learn more about bash, my studies in shell are far behind me :)
Thank you in advance!
zet
Solution
This might work for you (GNU sed):
sed -E 's/(set Nex)"([0-9]+)"/echo \1\\"$(echo "scale=2;\2\/2"|bc)\\"/e' file
For the "
's to be literal when echoed they must be escaped \"
's. Then the escapes must be escaped for the evaluation of the echo command by the e
flag of the substitution \\"
.
Answered By - potong Answer Checked By - Willingham (WPSolving Volunteer)