-1

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

  • As you were already told in a previous thread, this sequence doesn't make any sense: `echo | bc | awk "BEGIN {print 180/$n1}"`. – lcd047 Jun 25 '15 at 07:29
  • 1
    Maybe you should rephrase your question to a more precise title, since you are posting with the *bash* tags, which makes people think of *variable* as a bash variable, not a variable assignment in an octave script. Remove all those things that are not necessary to have in your script. In principle you are trying to replace an entire line or variable assignment in a octave file. right? – jhoepken Jun 25 '15 at 07:29

1 Answers1

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.

jhoepken
  • 1,743
  • 2
  • 16
  • 22
  • So it really should be `sed -i -e "s/alpha = .*/alpha = -pi\/${n2}/g" makemesh_rot.m` but still i get `sed: -e expression #1, char 37: unterminated address regex` – Osher Goldman Jun 25 '15 at 07:40
  • Actually that just worked. Thanks – Osher Goldman Jun 25 '15 at 07:43
  • Did it solve your problem, @OsherGoldman ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See [Help Center > Asking](http://stackoverflow.com/helpcenter/someone-answers) if you have any question! – fedorqui 'SO stop harming' Jun 25 '15 at 09:05