[SOLVED] open two text files , read them line by line and update parameters of the 3rd file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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
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.
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'
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
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.