Fernando Martinez
Fernando Martinez

Reputation: 549

Using awk to find min and max?

I have a large file that contains many types of lines. One of the types is lines of this form:

The lines all start with ATOM, and the 7th-9th fields are the x, y, and z values of the specified atom. How can I use awk to find all ATOM lines and then calculate the min and max of the x, y, and z values?

This is my file: http://pastebin.com/EqA2SUMy

One of the types is lines of this form:

ATOM      1  N   ASP A 435       7.397  28.376 121.784  1.00 34.35           N  
ATOM      2  CA  ASP A 435       8.023  27.301 122.545  1.00 30.66           C  
ATOM      3  C   ASP A 435       8.170  27.721 124.009  1.00 31.39           C  
ATOM      4  O   ASP A 435       9.078  28.509 124.284  1.00 38.78           O  

Can anyone show me how to do this please?

Upvotes: 1

Views: 472

Answers (1)

Zombo
Zombo

Reputation: 1

#!awk -f
BEGIN {
  min7 = min8 = min9 = 1000
}
$1 == "ATOM" {
  if ($7 < min7)
    min7 = $7
  if ($8 < min8)
    min8 = $8
  if ($9 < min9)
    min9 = $9
  if ($7 > max7)
    max7 = $7
  if ($8 > max8)
    max8 = $8
  if ($9 > max9)
    max9 = $9
}
END {
  print min7, min8, min9
  print max7, max8, max9
}

Upvotes: 2

Related Questions