sed用变量替换 [英] sed replace with variable
本文介绍了sed用变量替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用正确的计算值(以给定角度的弧度,即变量n1)替换表示 alpha = -pi/...
的行
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
我环顾四周,尝试了几种变体,将"s/alpha =.*/$ n3/"
更改为's/alpha =.*/"$ n3"/
和其他几种方法,就是用 $ n3
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
推荐答案
我非常定期地执行以下操作:
I do this very regularly as follows:
sed -i -e "s/alpha = .*/alpha = ${n3}/g" makemesh_rot.m
-i
参数直接在原位替换事物,从而使临时文件过时.
The -i
parameter replaces things directly in-place, which renders the temporary file obsolete.
这篇关于sed用变量替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文