Search code examples
unixminimum

Unix command: Find minimum from column, and do something


I have following input files

 SCFE: SCF energy: UDFT(b3lyp)     -7255.77607552893 hartrees   iterations:  36
 SCFE: SCF energy: UDFT(b3lyp)     -7256.47180169446 hartrees   iterations:  13
 SCFE: SCF energy: UDFT(b3lyp)     -7257.05043442327 hartrees   iterations:  25
 SCFE: SCF energy: UDFT(b3lyp)     -7257.53756596970 hartrees   iterations:   7
 SCFE: SCF energy: UDFT(b3lyp)     -7257.94483191615 hartrees   iterations:   7
 SCFE: SCF energy: UDFT(b3lyp)     -7258.28358389906 hartrees   iterations:   7
 SCFE: SCF energy: UDFT(b3lyp)     -7258.56278138629 hartrees   iterations:   7
.....

From this file, I hope extract the minumum value of 5th column of this file, and do following calculation

cat input.dat | awk '{print ($5 - minimum of 5th column)*627.509}' > output

But I'm not sure how can I find, store, and use the minimum value of 5th column. Thank you very much in advance :)


Solution

  • You need two passes to do ($5 - minimum of 5th column)*627.509 for each row

    Try this:

    awk 'NR==FNR{min=min<$5?min:$5;next}{$5=($5-min)*627.509}1' OFS='\t' file file
    

    $ cat file
    SCFE: SCF energy: UDFT(b3lyp)     -7255.77607552893 hartrees   iterations:  36
    SCFE: SCF energy: UDFT(b3lyp)     -7256.47180169446 hartrees   iterations:  13
    SCFE: SCF energy: UDFT(b3lyp)     -7257.05043442327 hartrees   iterations:  25
    SCFE: SCF energy: UDFT(b3lyp)     -7257.53756596970 hartrees   iterations:   7
    SCFE: SCF energy: UDFT(b3lyp)     -7257.94483191615 hartrees   iterations:   7
    SCFE: SCF energy: UDFT(b3lyp)     -7258.28358389906 hartrees   iterations:   7
    SCFE: SCF energy: UDFT(b3lyp)     -7258.56278138629 hartrees   iterations:   7
    

    $ awk 'NR==FNR{min=min<$5?min:$5;next}{$5=($5-min)*627.509}1' OFS='\t' file file
    SCFE:   SCF energy: UDFT(b3lyp) 1748.68 hartrees    iterations: 36
    SCFE:   SCF energy: UDFT(b3lyp) 1312.11 hartrees    iterations: 13
    SCFE:   SCF energy: UDFT(b3lyp) 949.011 hartrees    iterations: 25
    SCFE:   SCF energy: UDFT(b3lyp) 643.332 hartrees    iterations: 7
    SCFE:   SCF energy: UDFT(b3lyp) 387.769 hartrees    iterations: 7
    SCFE:   SCF energy: UDFT(b3lyp) 175.199 hartrees    iterations: 7
    SCFE:   SCF energy: UDFT(b3lyp)       0 hartrees    iterations: 7