exsonic01
exsonic01

Reputation: 637

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 :)

Upvotes: 1

Views: 235

Answers (3)

jaypal singh
jaypal singh

Reputation: 77085

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

Upvotes: 2

James McPherson
James McPherson

Reputation: 2556

Did you want to print ($5 - minimum of 5th column)*627.509 for each row in the table? I don't think you can do that, without two scans of the table. However, if you only want to find the minimum value of that column and multiply it by 627.509 (where does that number come from, btw?) then that's easy:

$ awk '{ if ($5 < lowest) {lowest = $5}} END {print lowest * 627.509}' input.dat 

$ MINVAL=`awk '{ if ($5 < lowest) {lowest = $5}} END {print lowest}' input.dat`
$ awk '{nval=($5-"$MINVAL")*627.509; print nval}' input.dat

Upvotes: 1

Amit
Amit

Reputation: 20456

You can use awk to find min for a field and then use it for further calculations.

awk 'NR == 1 || $5 < min {min = $5}END{print min * 627.509}'  input.dat > output

Upvotes: 0

Related Questions