[SOLVED] Copy a number from a text file to another in a script.
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.
I need to copy the value of p aver.=0.921167 (beginning of line 16) in the following file Plot, replacing the value of pr in line 11:
Code:
set title "PAKD-A"
set xrange[0:1.0]
set yr [0:6]
set format x "%.1f"
set format y "%.1f"
set xlabel 'x/Bx'
set ylabel 'C_{p}=(p_{01}-p)/(p_{01}-p_1)'
set key top left
pr=0.919725
pr_old=0.919677
pr_lau=0.919368
plot 'press' u ($1):((1.-($2))/(1.-pr)) w l lw 2 lc rgb 'black' t "LKE",\
'press_60' u ($1):((1.-($2))/(1.-pr_old)) w l lw 2 lc rgb 'red' t "60",\
'press_lau' u ($1):((1.-($2))/(1.-pr_lau)) w l lw 2 lc rgb 'violet' t "TRAF",\
'./Cp_Exp_Re_150000' u 1:2 pointtype 7 pointsize 1 lc rgb 'green' t "EXP"
set term post eps enhan solid color lw 2 "Helvetica" 24
set out 'cp.eps'; replot
set out ; set term wxt ; set size 1.,1. ; replot
Is it possible to do it in a common Linux script?
Last edited by +lorenzo90+; 12-24-2014 at 07:48 AM.
Since today I've done it manually copying and pasting the number between the two files using vi. But I have to do it very often so I'd like to find a way to do it automatically in the script I use to run the code. I'm working on my master's degree thesis in mechanical engineering and it's the first time I use linux; in the past I've only used MATLAB code so I'm not used to working with text files. Looking for a solution on the net I've found this forum.
Since today I've done it manually copying and pasting the number between the two files using vi. But I have to do it very often so I'd like to find a way to do it automatically in the script I use to run the code. I'm working on my master's degree thesis in mechanical engineering and it's the first time I use linux; in the past I've only used MATLAB code so I'm not used to working with text files. Looking for a solution on the net I've found this forum.
The problem description is confusing (to me). Perhaps the problem stems from terminology.
Your original post contains an inlet section and an outlet section. Are these two distinct files or simply text headers in a single input file? How many files are you talking about, including the desired output file? Is this problem a simple matter of copying the input file to the output file except deleting one specific line, and in place of that line a copy of everything in a second input file? It might help if you give names to each of these files and show each in a different "code box." Any section of text in your post may be shown in a "code box" by highlighting it and then clicking on the pound sign (a/k/a hash mark) icon.
As Daniel B. Martin suggested I put the two files in 'code boxes'. The first and bigger one is the inout file, while the second one is the Plot file (needed to plot the results with gnuplot).
Using the command:
Code:
grep -w "p aver." inout >temp
I managed to create the file temp containing only this line:
Code:
p aver. = 0.920042 dim.= 100.954803 [kPa]
Now I should find a way to change the previous line in:
Code:
pr=0.920042
And then overwrite it in the 11th line on the file Plot.
Several correct solutions have already been posted.
OP, you may tighten your code by using >> which means "append to existing file."
That eliminates the need for temporary files.
With InFile1 ...
Code:
Once upon a midnight dreary, while I pondered weak and weary,
Over many a quaint and curious volume of forgotten lore,
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
''Tis some visitor,' I muttered, 'tapping at my chamber door -
Only this, and nothing more.'
... and InFile2 ...
Code:
Inserted text, line 1
Inserted text, line 2
Inserted text, line 3
... the one line containing "While I nodded" in InFile1 is replaced by
the entire InFile2 with this sequence ...
Code:
sed -n '1,/While I nodded/p' $InFile1 |sed '$d' >$OutFile
cat $InFile2 >>$OutFile
sed -n '/While I nodded/,$p' $InFile1 |sed '1d' >>$OutFile
... to produce this OutFile ...
Code:
Once upon a midnight dreary, while I pondered weak and weary,
Over many a quaint and curious volume of forgotten lore,
Inserted text, line 1
Inserted text, line 2
Inserted text, line 3
As of some one gently rapping, rapping at my chamber door.
''Tis some visitor,' I muttered, 'tapping at my chamber door -
Only this, and nothing more.'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.