Find the answer to your Linux question:
Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By mrbruno
Dear awk/bash guru, Here is the file which we would like to obtain the TIME(ps) vs DV/DL information and calculate the cumulative DV/DL. Here is output "${pre0}_prod_v0_l${X}_${A}.out". When X=1; ${A}=1~10 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    24

    awk cumulative average/include wilde card


    Dear awk/bash guru,

    Here is the file which we would like to obtain the TIME(ps) vs DV/DL information and calculate the cumulative DV/DL.

    Here is output "${pre0}_prod_v0_l${X}_${A}.out".

    When X=1; ${A}=1~10

    When X=2; ${A}=1~10

    ...

    When X=10; ${A}=1~10
    Code:
    NSTEP =      250   TIME(PS) =      50.250  TEMP(K) =   295.76  PRESS =  -215.7
     Etot   =     -7764.8430  EKtot   =      3002.7461  EPtot      =    -10767.5890
     BOND   =      2122.1691  ANGLE   =        13.8020  DIHED      =         4.9113
     1-4 NB =         9.7072  1-4 EEL =        21.5693  VDWAALS    =      2214.9972
     EELEC  =    -15154.7452  EHBOND  =         0.0000  RESTRAINT  =         0.0000
     DV/DL  =        -0.5972
     EKCMT  =       987.1029  VIRIAL  =      1142.1202  VOLUME     =     33281.4023
                                                        Density    =         1.0260
     Ewald error estimate:   0.7836E-04
     ------------------------------------------------------------------------------
    
    
     NSTEP =      500   TIME(PS) =      50.500  TEMP(K) =   297.03  PRESS =   199.5
     Etot   =     -7714.8112  EKtot   =      3015.5875  EPtot      =    -10730.3986
     BOND   =      2184.8452  ANGLE   =        16.0653  DIHED      =         4.4046
     1-4 NB =         9.5927  1-4 EEL =        20.3086  VDWAALS    =      2284.2392
     EELEC  =    -15249.8542  EHBOND  =         0.0000  RESTRAINT  =         0.0000
     DV/DL  =        -0.7104
     EKCMT  =       977.6539  VIRIAL  =       834.2227  VOLUME     =     33298.0519
                                                        Density    =         1.0255
     Ewald error estimate:   0.4997E-04
     ------------------------------------------------------------------------------
    Idea results:

    Code:
    TIME        DV/DL     cumulative_DV/DL
    50.250     -0.5972    -0.5972 
    50.500     -0.7104    -0.6538
    ...             ...            ....
    
    *note: -0.6538=-(0.5972-0.7104)/2
    So the first issue is that

    Code:
    awk '$1 ~ /^NSTEP*/ { printf("%14.3f",$6)} $1 ~ /^DV/DL/ { printf("%14.3f\n",$3)}'
    The /^DV/DL/ will bring error due to /^DV/DL/ . If I use /^DV*/, it will also print the information of the "Density" line. May we know if there is any way in awk to include "/" or deal with this issue?


    The next question is code simplification:

    for X in 1

    There are ${pre0}_prod_v0_l${X}_${A}.out ; A=1~10

    We want to process cumulative average of A=1, A=1~2, A=1~3, A=1~4,...A=1~10, so here is our code. If it's not too much asking, we wonder if any guru could simplify the code?

    Code:
    for ((X=1; X<=1; X++))
    
    do
    
       for ((A=1; A<=10; A++))
    
       do
    
    
    # get DV/DL over time 
    
    awk '$1 ~ /^NSTEP*/ { printf("%14.3f",$6)} $1 ~ /^DV/DL/ { printf("%14.3f\n",$3)}' ${pre0}_prod_v0_l${X}_${A}.out  > awk_dvdl_${pre0}_v0_l${X}_${A}.dat
    
    # The last three "dv/dl" of each file are useless so we have to remove them.
    
    head -n -3 awk_dvdl_${pre0}_v0_l${X}_${A}.dat > awk_dvdl_${pre0}_v0_l${X}_${A}_remove_3.dat
    
       done
    
    # Here we have to process the file like this:
    
    # cat awk_dvdl_${pre0}_v0_l${X}_1_remove_3.dat > awk_dvdl_${pre0}_v0_l${X}_1.dat
    
    # cat awk_dvdl_${pre0}_v0_l${X}_1_remove_3.dat awk_dvdl_${pre0}_v0_l${X}_2_remove_3.dat > awk_dvdl_${pre0}_v0_l${X}_2.dat
    
    # cat awk_dvdl_${pre0}_v0_l${X}_1_remove_3.dat awk_dvdl_${pre0}_v0_l${X}_2_remove_3.dat awk_dvdl_${pre0}_v0_l${X}_3_remove_3.dat> awk_dvdl_${pre0}_v0_l${X}_3.dat
    
    # etc...
    
    #make variables clean
    var1=""
    var2=""
    var3=""
    
       for ((A=1; A<=10; A++))
    
       do
    
    var1="cat"
    var2="$var2 awk_dvdl_${pre0}_v0_l${X}_${A}_remove_3.dat"
    var3="awk_dvdl_${pre0}_v0_l${X}_${A}.dat"
    
     echo "$var1 $var2  >> $var3"
    
       done > awk_dvdl_v0_total.sh
    
    chmod 700 awk_dvdl_v0_total.sh
    
    sh awk_dvdl_v0_total.sh
    
    
       for ((A=1; A<=10; A++))
    
       do
    
    # calculate cumulative average
    
    awk '{printf("%14.3f",$1)} {printf("%14.3f",$2)}{DVDL+=$2} {printf("%14.3f\n",DVDL/NR)}' awk_dvdl_${pre0}_v0_l${X}_${A}.dat > awk_dvdl_${pre0}_v0_l${X}_${A}_final.dat
    
       done
    
    done
    Please forgive any beginner's ignorance, if any.

  2. #2
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    66
    Quote Originally Posted by psu4 View Post
    May we know if there is any way in awk to include "/" or deal with this issue?
    The first question is pretty easy - simply escape the slash so it looses its special meaning in that use: /^DV\/DL/

    I'm afraid I'm not following what you're doing in the second part. It might help if you described the inputs and outputs.
    psu4 likes this.

  3. #3
    Just Joined!
    Join Date
    Aug 2012
    Posts
    24
    Thanks mrbruno! It works perfectly! -Henry

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •