LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2010, 12:33 PM   #1
jdaoutid
LQ Newbie
 
Registered: Mar 2010
Location: Brussels
Posts: 3

Rep: Reputation: 0
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
 
Old 03-25-2010, 02:22 PM   #2
Vrajgh
Member
 
Registered: Aug 2005
Posts: 68

Rep: Reputation: 33
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.

Last edited by Vrajgh; 03-25-2010 at 02:25 PM. Reason: Reading the original post again.
 
Old 03-26-2010, 05:24 AM   #3
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
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.
 
Old 03-26-2010, 11:23 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: Modify file worm5252 Programming 4 03-18-2010 08:26 AM
[SOLVED] [bash] read file, modify, write - possible without tmpfile? hashbang#! Programming 2 02-11-2010 05:27 AM
bash Shell script to modify DNS zone file KinnowGrower Programming 4 12-12-2009 08:07 PM
How do you define a 2 digit numeric counter? ya3784 Linux - General 3 07-16-2007 03:09 PM
How to define Text box as numeric Linh Programming 1 09-24-2003 03:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration