问题
I want to replace a line that says alpha = -pi/...
with the correct calculated value in radians of the angle given ie variable n1
#!/bin/bash
read -p "Angle in degrees : " n1
## Convert angle to radians
printf -v "n2" "%.0f" $(echo | bc | awk "BEGIN {print 180/$n1}")
echo "$n1 Degrees = pi/$n2"
printf -v "n3" "alpha = -pi/$n2;"
echo "${n3}"
cd /home/src/octave_scripts/makemesh_rot
sed "s/alpha =.*/$n3/" makemesh_rot.m > makemesh_rot.m_temp
Ive had a look around and tried a few variations changing "s/alpha =.*/$n3/"
to 's/alpha =.*/"$n3"/'
and a few other ways all that does is substitues alpha = -pi/...
with $n3
回答1:
I do this very regularly as follows:
sed -i -e "s/alpha = .*/alpha = ${n3}/g" makemesh_rot.m
The -i
parameter replaces things directly in-place, which renders the temporary file obsolete.
来源:https://stackoverflow.com/questions/31043790/sed-replace-with-variable