1

I have a file named variables.f90 , having many lines defining different variables, as follows ::

integer::n_monomer=6800
real*8::rx=5.0d0
#... randomly integer and real numbers defined
real*8::mu_nano=8.0d0
.......
......

and I dont know what will be the value of mu_nano, it can be any real number. Now I want to modify the above statement such that, its value is incremented by 1 using bash script as follows ::

real*8::mu_nano=9.0d0
0

2 Answers 2

2

This looks like fortran code, and it is not the greatest idea ever to parse higher language source code, but anyway... with awk:

awk -F'[=.]' '/nano/{$2++; print $1"="$2"."$3; next}1'

This assumes that variable is always given with a dot. It takes only integer part of it (between = and .), increase by 1, and prints everything back.

0
2

To edit in place, I'd use perl:

perl -i.bak -pe 's/(?<=mu_nano=)([\d.]+)/ sprintf "%.1f", $1+1 /e' variables.f90
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .