sed replace with variable [duplicate]

£可爱£侵袭症+ 提交于 2021-02-17 07:10:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!