Help about using variables of float numbers in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help about using variables of float numbers in sed
# 1  
Old 12-01-2017
Help about using variables of float numbers in sed

Hi, I need to run a Fortran program which reads a input file with a fixed name many times, each time I need to change a number (real) in that input file, this is how I currently do it and I know it is not elegent at all:

Code:
cp inputfile.dat backup.dat
sed -i 's/28.0/0.01/g' inputfile.dat
./myCode

cp backup.dat inputfile.dat
sed -i 's/28.0/1.0/g' inputfile.dat
./myCode

cp backup.dat inputfile.dat
sed -i 's/28.0/2.0/g' inputfile.dat
./myCode

cp backup.dat inputfile.dat
sed -i 's/28.0/3.0/g' inputfile.dat
./myCode

cp backup.dat inputfile.dat
sed -i 's/28.0/4.0/g' inputfile.dat
./myCode


I am looking for an elegent way, may be something like this:

Code:
#!/bin/bash

cp inputfile.dat backup.dat
for((i=0; i<100;i++));
do 
    cp backup.dat inputfile.dat
    sed -i 's/28.0/"$i/10.0"/g' inputfile.dat
    ./myCode
done

The problem is in the line with sed: I don't know how to use the index "i", manipulate it like "$i/10.0" here, and use it in sed.

I know there must be some solution. Thank you very much for helps!

about the system: I am using Windows 10 (yes, I have to use windows!) with cygwin, the shell is bash.

thanks again!

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-01-2017 at 05:41 AM.. Reason: Added CODE tags.
# 2  
Old 12-01-2017
Welcome to the forum.

Unfortunately, neither bash nor sed provide floating arithmetics. Would an awk solution help you?
Code:
mv inputfile.dat backup.dat
for ((i=0; i<100; i++))
  do     awk -v IX=$i '1+gsub (/28.0/, IX/10)' backup.dat > inputfile.dat 
  done

It would NOT cover your first, 0.01 case, though.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-01-2017
Thanks! It solved my problem!
# 4  
Old 12-01-2017
As RudiC said, the bash shell doesn't do floating point calculations in arithmetic expansions. The Korn shell does (i.e., ksh), but I don't know if it is available on cygwin.

And, as RudiC implied, your sample changes do not match your script. Your sample script shows starting at 0.01, incrementing by .99, and then three increments of 1. If you are trying to replace the string 28.0 with a numbers starting with 0.0 and anding with 9.9 in steps of .1 you could also try:
Code:
cp input.dat backup.dat
for ((i=0; i<100; i++))
do	rep=$(printf '%d.%d' $((i / 10)) $((i % 10)))
	sed "s/28[.]0/$rep/g" backup.dat > input.dat
	./myCode
done
# Run the following mv if you want to leave input.dat the way it was when you started.
mv backup.dat input.dat

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing multiple variables containing numbers

a=1 456 b=4928 c=23 d=456 I want to compare four variables to get the name of the variable having the highest number (2 Replies)
Discussion started by: proactiveaditya
2 Replies

2. Shell Programming and Scripting

using sed command to display contents where line numbers are stored in variables

if i want to display the contents of a file between say line number 3 and 10 then i use the following command sed -n '3,10p' filename if this 3 was contained in x and 10 was contained in y then how wud this command modified? sed -n '$x,$yp' filename does not work..please advise (2 Replies)
Discussion started by: arindamlive
2 Replies

3. Shell Programming and Scripting

Random float numbers in BASH

Hi people :) I'm learning shell scripting using bash and I want to generate 4 floating point number with 5 decimal places and write them to a file and a variable. I've done all this except the $RAMDOM enviroment variable does not generate a float number but a integrer. I hope you could... (3 Replies)
Discussion started by: pharaoh
3 Replies

4. Programming

comparison between float numbers

Hi, i have a simple control like this: if(sum>1.0)... If i try to print sum i get 1.000000 but the check returns true. I think it depends on float precision. How can i modify the check? thanks (1 Reply)
Discussion started by: littleboyblu
1 Replies

5. Shell Programming and Scripting

Can i use Variables in sed command in line numbers

I wish to give line number from one point to another in sed command like this sed -n 1,1000000p file1.txt >file2.txt but variable line number $x,$x+100000 can i give it cos i tried and it was giving an error any suggestions?/ Thx in advance AC (2 Replies)
Discussion started by: bezudar
2 Replies

6. Solaris

can array store float point numbers

Hi all, I have doubt can array in a shell script can store floating point numbers. i have tired. but i unable to work it out. Please help me regarding this Thank U Naree (1 Reply)
Discussion started by: naree
1 Replies

7. Solaris

i cannot assign float point numbers to an array in solaris

total=0 declare -a sum limit=`iostat -En | grep -i size | awk '{print $2}' | sed -e 's/GB//g' | wc -l` echo "Limit is equal to $limit" ara="`iostat -En | grep -i size | awk '{print $2}' | sed -e 's/GB//g'`" for (( i=1; i<=$limit; i++ )) do sum=`echo $ara | cut -d " " -f $i` echo ${sum}... (11 Replies)
Discussion started by: naree
11 Replies

8. Programming

math.h: float ceilf(float x)

Good morning, I'm testing the use of ceilf: /*Filename: str.c*/ #include <stdio.h> #include <math.h> int main (void) { float ceilf(float x); int dev=3, result=0; float tmp = 3.444f; printf("Result: %f\n",ceilf(tmp)); return 0; } (1 Reply)
Discussion started by: jonas.gabriel
1 Replies

9. Shell Programming and Scripting

adding float numbers

how to add 2 float values to each other? i tried this and it doesnt work: #!bin/bash numone=1.234 numtwo=0.124 total=`expr $numone + $numtwo` echo $total thanks (5 Replies)
Discussion started by: strike
5 Replies
Login or Register to Ask a Question