LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   In BASH: How to read + define + modify a numeric value from/to a file. (https://www.linuxquestions.org/questions/programming-9/in-bash-how-to-read-define-modify-a-numeric-value-from-to-a-file-797839/)

jdaoutid 03-25-2010 12:33 PM

In BASH: How to read + define + modify a numeric value from/to a file.
 
Hallo folks,
I want enlightenment in my problem.
Taske 1) I want my bash file to read from "input.dat" the two values emin emax. My input file looks like that:
#cat input.dat
!Energies
emin 10.00 !minimum energy
emax 30.00 !maximum energy


Now this seems to be not so hard with the command awk
#!/bin/bash
awk '{FS=" "}/emin/{print $2}' input.dat
awk '{FS=" "}/emax/{print $2}' input.dat

The result was
10.00
30.00

So far so good.
Task 2) Now, I want to define two variables (e.g. e1,e2) in the bash file, so that their values would correspond to 00.00 and 30.00, as read from the input file. This one I have not found yet, thus asking for your advice. At the end, writing echo $e1 $e2, I should get
10.00 30.00

Task 3) This is even harder to me: I want to replace the values emin,emax in a new file "modify.dat" which looks like that:
...
c---- energy interval
emin = 1.00
emax = 2.00
...

with the values e1 and e2 I have in my bash file. In other words, I want to call "modify.dat", find these two lines and replace the numeric values with the e1 and e2. At the end, my file should be like:
...
c---- energy interval
emin = 10.00
emax = 30.00
...


How is this possible?
Thanks a lot,
John

Vrajgh 03-25-2010 02:22 PM

Does this have to be done in bash?

You are calling awk in order to extract the numbers already. I suggest you write an awk script to do this because it would be really easy in awk and probably more complicated in bash!

You already have the rules to find the lines of interest from awk, so just add the change of number to those lines. A quick example which I haven't tested and probably contains some basic mistakes:
Code:

#!/bin/awk -f
/emin/ {$2=10.00}
/emax/ {$2=30.00}
{print}

I appologise if the point of your post was to learn bash and this isn't what you want.

EDIT:
Or are you trying to read the new values from another file? It might be best if you provide a larger example of the format of your input files and expected output.

PMP 03-26-2010 05:24 AM

To collect the variables in e1 and e2
Code:


e1=`awk '{FS=" "}/emin/{print $2}' input.dat` (Mind these are backticks)
e2=`awk '{FS=" "}/emax/{print $2}' input.dat` (Mind these are backticks)

For replacement of values
Code:

sed 's/emin =.*/emin = $e1/' modify.dat
sed 's/emax =.*/emax = $e1/' modify.dat

I have not tested it. Just an idea to achieve the mentioned task.

grail 03-26-2010 11:23 AM

Here it is put together:

Code:

#!/usr/bin/awk -f

BEGIN{
    file1=ARGV[1]
    file2=ARGV[2]
}

getline < file1{

    if(/emin/)
        {min=$2}
    if(/emax/)
        {max=$2}
}

END{
    print | "sed -i -e 's/\\(emin.*= \\).*/\\1 "min"/' -e 's/\\(emax.*= \\).*/\\1 "max"/' "file2
}



All times are GMT -5. The time now is 11:59 PM.