LinuxQuestions.org
Visit Jeremy's Blog.
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 10-18-2010, 10:52 PM   #1
rastin_nz
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Rep: Reputation: 0
Red face open two text files , read them line by line and update parameters of the 3rd file


Hi everyone;
I have two txt files containing x and y coordinates: xcoord.txt & ycoord.txt
I need to open them; read them line by line to get each coordinate;
then each time I need to update Xs and Ys parameters inside another file called "dc.in" with the grabbed values.
Finally each time I need to run two exe files ( dc_2002 and st_vac) and produce corresponding output for each Xs and Ys ( dc.in is an input file for this exe files)
I have written the following code but it does not work:
#!/bin/bash
stn="edrz"
mt="mt"
vel="vel"
for i in `seq 1 2`;
do
vi dc.in
read myxcoord[$i]< coordinatex.txt
echo ${myxcoord[i]}
read myycoord[$i]< coordinatey.txt
echo ${myycoord[i]}
Xs=${myxcoord[i]}
Ys=${myycoord[i]}
Zs=5;
./dc_2002
./st_vac
cp motion.out "$i$stn$mt"
cp time.out "$i$stn$vel"
rm motion.out
rm time.out
done

your help will rescue my PhD and much appreciated.
Sepi
 
Old 10-19-2010, 01:38 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Firstly, please use [code][/code] tags so we can better read and understand your code.

How about something like:
Code:
#!/bin/bash

stn="edrz"
mt="mt"
vel="vel"

while read -r myxcoord myycoord
do
    echo $myxcoord > dc.in
    echo $myycoord >> dc.in

    Xs=$myxcoord
    Ys=$myycoord
    Zs=5

    ./dc_2002
    ./st_vac
    cp motion.out "$i$stn$mt"
    cp time.out "$i$stn$vel"
    rm motion.out
    rm time.out
done< <(paste coordinatex.txt coordinatey.txt)
Untested of course and I have also never seen vi used in this way so not sure what exactly is to be written into the file??
 
1 members found this post helpful.
Old 10-19-2010, 04:03 PM   #3
rastin_nz
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Lightbulb

Hi grail,
thanks a lot for your help;
it gives me back the following error:
./test: line 20: done < < coordinatex.txt coordinatey.txt
the name of my text files are "coordinatex.txt" and "coordinatey.txt "
on the other note I have another question too:

./dc_2002 produces motion.out that should be used by ./st_vac; one thing is that the ./dc_2002 takes a bit of time to be fully executed. therefore when I just removed the aforementioned error (by putting one of the text file instead of two of them which is not correct and I just wanted to make it correct temporarily to go to the next line) since the motion.out file has not been produced the next lines will return errors too; it says that motion.out and time.out are not available in the directory; Is there any way that I can make it conditional..i.e I go to the next lines when I am sure that ./dc_2002 and ./st_vac have produced their outputs
thanks a lot
 
Old 10-19-2010, 05:28 PM   #4
rastin_nz
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Hi there, the following code works but not in the way that I want it:




#!/bin/bash
stn="edrz"
mt="mt"
vel="vel"

while read -r myxcoord myycoord
do
echo $myxcoord > dc.in
echo $myycoord >> dc.in

Xs=${myxcoord}
Ys=${myycoord}
Zs=5

./dc_2002
./st_vac


cp motion.out "$i$stn$mt"
cp time.out "$i$stn$vel"
rm motion.out
rm time.out
done < <( paste coordinatex.txt coordinatey.txt)



the file dc.in is given in the folllowing:
&IN NNL=5,
TTH=1,2,3,4,5,
VPP=2.4,4.98,5,5.91,6.55,7.5,
VSS=1.38,2.87,2.89,3.41,3.78,4.33,
DDEN=2,2.3,2.45,2.65,2.75,3.0,
Xs=-21.1289,Ys=20.7020,Zs=5,delta=0.02,
SLIP=0.5,
DIP=60.,
AZ=45.,
RAKE=-90.0,
NT=2048,
TL=40.96,
FREQ0=0.,
EPS=0.0000001,
&END


when I run the previous cod , I will only have for example two numbers in my dc.in file
like:
21
-21

However I just want to replace the value read from the files into Xs and Ys cos the whole file is needed for running the FOrtran Exe files
I do not know how I can just replace the values of Xs and Ys and Zs without any other changes
Thanks in adavance
 
Old 10-19-2010, 07:18 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Firstly, please use tags so we can better read and understand your code. AGAIN

I am guessing you figured out why you received the following error:
Quote:
./test: line 20: done < < coordinatex.txt coordinatey.txt
Quote:
However I just want to replace the value read from the files into Xs and Ys cos the whole file is needed for running the FOrtran Exe files
I do not know how I can just replace the values of Xs and Ys and Zs without any other changes
This would have been extremely valuable information in your original question as the code you presented has nothing to do with this idea

Try placing a sed in your script to change the file instead of creating it each time.
Example:
Code:
sed -i "s/Xs=[^,]*/Xs=${myxcoord}/" dc.in
Quote:
./dc_2002 produces motion.out that should be used by ./st_vac; one thing is that the ./dc_2002 takes a bit of time to be fully executed. therefore when I just removed the aforementioned error (by putting one of the text file instead of two of them which is not correct and I just wanted to make it correct temporarily to go to the next line) since the motion.out file has not been produced the next lines will return errors too; it says that motion.out and time.out are not available in the directory; Is there any way that I can make it conditional..i.e I go to the next lines when I am sure that ./dc_2002 and ./st_vac have produced their outputs
Try using the followin format:
Code:
./dc_2002 && ./st_vac
This will only run the second executable if the first has finished successfully.
 
1 members found this post helpful.
Old 10-19-2010, 07:21 PM   #6
rastin_nz
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Hi Grail,
I am very new in in filed can you please tell me how I can put tag ?
 
Old 10-19-2010, 07:34 PM   #7
rastin_nz
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Hi again,
I execute the following script but it makes dc.in empty (
#!/bin/bash
stn="edrz"
mt="mt"
vel="vel"

while read -r myxcoord myycoord
do
echo $myxcoord
echo $myycoord
sed -i "s/Xs=[^,]*/Xs=${myxcoord}/" dc.in
sed -i "s/Ys=[^,]*/Ys=${myycoord}/" dc.in
./dc_2002 && ./st_vac

cp motion.out "$i$stn$mt"
cp time.out "$i$stn$vel"
rm motion.out
rm time.out
done < <( paste coordinatex.txt coordinatey.txt)


 
Old 10-19-2010, 09:02 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
I am very new in in filed can you please tell me how I can put tag ?
Type in exactly the tags I gave you, one before the start and one at the end or you can go to the advanced tab and select it from the menu bar at the top.
Quote:
I execute the following script but it makes dc.in empty (
Not to sound funny, but are you sure it had anything in to start with?

Try this small change as a test:
Code:
sed -i "s/Xs=[^,]*/Xs=${myxcoord}/" dc.in

# to

sed -i.bak "s/Xs=[^,]*/Xs=${myxcoord}/" dc.in
It works as expected for me. This change will make a backup called -- 'dc.in.bak'
 
1 members found this post helpful.
Old 10-20-2010, 12:00 AM   #9
rastin_nz
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Hi Grail and just thanks a million time.. it works perfectly ))) I would buy a drink for you if you were here in Auckland mate


[/#!/bin/bash
stn="edrz"
mt="mt"
vel="vel"

while read -r myxcoord myycoord
do
echo $myxcoord
echo $myycoord
sed -i.bak "s/ Xs=[^,]*/Xs=${myxcoord}/" dc.in
sed -i.bak "s/ Ys=[^,]*/Ys=${myycoord}/" dc.in

./dc_2002 && ./st_vac

cp motion.out "$i$stn$mt"
cp time.out "$i$stn$vel"
rm motion.out
rm time.out
done < <( paste coordinatex.txt coordinatey.txt)
]

I m not sure if I could put them in the tag..I m so happy ..just a little thing for you which is a very big deal for me:
As I want to produce different output for each (Xs, Ys) then I need to have a counter to put $i (counter) in the output file name;
can you please help me with it ?
Cheers,
Sepi
 
Old 10-20-2010, 06:32 AM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Smile ... about code tags

Hi,

glad that you solved your problem, but you still do not use code tags properly. This '[/' is not a code tag. This is how you use them:

[CODE]YOUR PROGRAM GOES IN BETWEEN HERE[/CODE]

Try it. Copy+Paste the part above and see how it works

Last edited by crts; 10-20-2010 at 06:33 AM.
 
1 members found this post helpful.
Old 10-20-2010, 07:24 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Counters are pretty easy:
Code:
counter=0

echo $counter

((counter++))

echo $counter
If you want to be tricky you can actually do it inline as well:
Code:
echo $((counter++))
echo $((counter++))

Last edited by grail; 10-20-2010 at 07:26 AM.
 
1 members found this post helpful.
Old 10-20-2010, 04:06 PM   #12
rastin_nz
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Code:
our program goes in between here
 
Old 10-20-2010, 04:17 PM   #13
rastin_nz
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Hi Grail,
I have written this code and counter is working but I have realized that inside dc.in only "Xs" is replaced with new values and for a weird reason Ys is not getting updated.

Code:
#!/bin/bash
stn="edrz"
mt="mt"
vel="vel"
i=0
while read -r  myxcoord myycoord
do
echo $myxcoord 
echo $myycoord
echo $i
sed  -i.bak "s/ Xs=[^,]*/Xs=${myxcoord}/" dc.in
sed  -i.bak  "s/ Ys=[^,]*/Ys=${myycoord}/" dc.in

./dc_2002 && ./st_vac
echo $((i++))
cp motion.out "$i$stn$mt"
cp time.out "$i$stn$vel"
rm motion.out
rm time.out
done  < <( paste  coordinatex.txt coordinatey.txt)






Can you please help me with this,
Cheers,
Sepi
 
Old 10-20-2010, 04:27 PM   #14
rastin_nz
LQ Newbie
 
Registered: Oct 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Hi Grail
Actually I set XS=0 and Ys=0 inside dc.in and execute the code ; just found that none of them have been updated (((
 
Old 10-20-2010, 04:37 PM   #15
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

Code:
sed  -i.bak "s/ Xs=[^,]*/Xs=${myxcoord}/" dc.in
sed  -i.bak "s/ Ys=[^,]*/Ys=${myycoord}/" dc.in
               ^ are those spaces a typo?
Does your file contain the spaces that I marked in your sed script? If not try
Code:
sed  -i.bak "s/Xs=[^,]*/Xs=${myxcoord}/;s/Ys=[^,]*/Ys=${myycoord}/" dc.in
You can replace both your sed statements with just the one above.
 
1 members found this post helpful.
  


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
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 01:42 PM
open text file and read line by line amani_87 Linux - Software 20 01-28-2010 10:25 PM
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 12:38 PM
read line by line form text file in java. spank Programming 1 10-18-2006 02:46 PM

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

All times are GMT -5. The time now is 11:38 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