LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Copy a number from a text file to another in a script. (https://www.linuxquestions.org/questions/programming-9/copy-a-number-from-a-text-file-to-another-in-a-script-4175529120/)

+lorenzo90+ 12-23-2014 02:45 PM

Copy a number from a text file to another in a script.
 
Hi,
I'm a new Linux user and I need your help.
The output of a CFD code is the following inout file:

Code:

inlet section:
 --------------
 grid inl. sec.      -0.350000  blade LE          -0.000006
 requested sec.      -0.350000  % from blade      0.349997
 stream. thick.      1.000000  stream radius      1.000000
 -------------
 mach abs. av.=      0.086502
 mach rel. av.=      0.086502
 mass flow    =      0.287789  dim.=      0.147395 [kg/s] 
 pt  aver.  =      0.999989  dim.=    101.324928 [kPa] 
 pte  aver.  =      0.999989  dim.=    101.324928 [kPa] 
 ptr  aver.  =      0.999989  dim.=    101.324928 [kPa] 
 ptre aver.  =      0.999989  dim.=    101.324928 [kPa] 
 ptrr aver.  =      0.999989  dim.=    101.324928 [kPa] 
 ro  aver.  =      1.000000  dim.=      1.204328 [kg/m^3]
 p    aver.  =      0.921167  dim.=    100.793755 [kPa] 
 t    aver.  =      1.000000  dim.=    293.149994 [K]   
 tt  aver.  =      1.000000  dim.=    293.149994 [K]   
 ttr  aver.  =      1.000000  dim.=    293.149994 [K]   
 ttrr aver.  =      1.000000  dim.=    293.149994 [K]   
 u    aver.  =      0.325113  dim.=      24.319593 [m/s] 
 v    aver.  =      0.227624  dim.=      17.027056 [m/s] 
 alfa abs. av.=    34.997253  [deg] 
 alfa rel. av.=    34.997253  [deg] 
 Re1  aver.  =  148576.6   
 
 outlet section:
 --------------
 grid out. sec.      1.400000  blade TE          0.999983
 requested sec.      1.400000  % from blade      0.330021
 stream. thick.      1.000000  stream radius      1.000000
 -------------
 mach abs. av.=      0.136238
 mach rel. av.=      0.136238
 mass flow    =      0.287782  dim.=      0.147392 [kg/s] 
 avdr aver.  =      1.000431
 pt  aver.  =      0.995486  dim.=    101.294586 [kPa] 
 pte  aver.  =      0.995486  dim.=    101.294586 [kPa] 
 ptr  aver.  =      0.995486  dim.=    101.294586 [kPa] 
 ptre aver.  =      0.995486  dim.=    101.294586 [kPa] 
 ptrr aver.  =      0.995486  dim.=    101.294586 [kPa] 
 ro2  aver.  =      1.000000  dim.=      1.204328 [kg/m^3]
 p2  aver.  =      0.800053  dim.=      99.977585 [kPa] 
 t2  aver.  =      1.000000  dim.=    293.149994 [K]   
 tt2  aver.  =      1.000000  dim.=    293.149994 [K]   
 ttr  aver.  =      1.000000  dim.=    293.149994 [K]   
 ttrr aver.  =      1.000000  dim.=    293.149994 [K]   
 u2  aver.  =      0.325253  dim.=      24.330078 [m/s] 
 v2  aver.  =    -0.533780  dim.=    -39.928589 [m/s] 
 alfa abs. av.=    -58.655205  mixed=    58.677368 [deg] 
 alfa rel. av.=    -58.655205  [deg] 
 Re2  aver.  =  233951.3      Re2s =  236631.3   
 Zwi          =  1.122890      Zwc  =  1.122890   
 
 mass flow error        = -2.3714351E-03  [%] 
 averaged mass flow    =  0.1473936      [kg/s]
 adiabatic  effic.      =  0.0000000E+00
 politropic effic.      =  0.0000000E+00
 tot. press. ratio      =  0.9954969   
 
 z(p)                  =  2.2522569E-02
 z=1-(v2/v2s)^2 ,etab  =  2.2522569E-02  0.9774774   
 z(T)=(T2-T2s)/(Tt1-T2s)=  2.2522569E-02
 (v2s/v2)^2-1          =  2.3041487E-02
 zetas                  =  2.3041487E-02
 Y1 =(pt1-pt2)/(pt1-p1) =  5.7129741E-02
 Y2 =(pt1-pt2)/(pt2-p2) =  2.3041606E-02  2.3240753E-02
 Y2s=(pt1-pt2)/(pt1-p2) =  2.2522647E-02  2.2522647E-02
 z_mix                  =  2.3152888E-02
 Y2s_mix                =  2.3152862E-02
 Y1_mix                =  5.8701873E-02
 v2  [m/s]              =  0.6251915      46.76652   
 v2s [m/s]              =  0.6323531      47.30223   
 p2mix [kPa]            =  0.8001435      99.97820   
 M2s                    =  0.1384237   
 Dpref [Pa]            =  -94586.10

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?

schneidz 12-23-2014 02:54 PM

what have you tried and where are you stuck.

it seems like grep/awk/sed/cut/paste/... is what you are after.

+lorenzo90+ 12-24-2014 05:07 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.

danielbmartin 12-24-2014 07:20 AM

Quote:

Originally Posted by +lorenzo90+ (Post 5289928)
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.

Daniel B. Martin

+lorenzo90+ 12-24-2014 07:46 AM

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.

schneidz 12-24-2014 09:58 AM

quick-and-dirty:
Code:

#man awk sed
#schneidz=`awk '/p    aver.  =/ {print $4}' lorenzo90.inout`
#sed -i s/pr=.*/pr=$schneidz/ lorenzo90.plot
#
#untested alternative:
sed -i s/pr=.*/pr=`awk '/p    aver.  =/ {print $4}' lorenzo90.inout`/ lorenzo90.plot


+lorenzo90+ 12-24-2014 10:25 AM

Thank you very much schneidz, your code is really brief but effective!!!
I finally managed to do it on my own with this code:
Code:

#
grep -w "p    aver." inout >temp
#
sed "s/ p    aver.  =      /pr=/" <temp >temp1
#
rm temp
#
mv temp1 temp
#
sed -e "s/d.*//" <temp >temp1
#
rm temp
#
mv temp1 temp
#
sed -e "s/pr=.*//" <Plot >Plot1
#
rm Plot
#
cat temp Plot1 >Plot
#
rm Plot1
#
rm temp
#

Surely your way it's better.

NevemTeve 12-24-2014 10:26 AM

a part could be (untested!):
Code:

set - $(grep '^ p    aver\.' "$infile")
NUM="$4"


+lorenzo90+ 12-24-2014 10:29 AM

Also your second alternative works.
Thank you so much again!!

Lorenzo

danielbmartin 12-24-2014 10:58 AM

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.'

Daniel B. Martin


All times are GMT -5. The time now is 03:21 AM.