awk search for max and min while ignoring special character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk search for max and min while ignoring special character
# 1  
Old 07-29-2015
awk search for max and min while ignoring special character

I am trying to get a simple min/max script to work with the below input. Note the special character (">") within it.

Script

Code:
awk  'BEGIN{max=0}{if(($1)>max)  max=($1)}END {print max}'
awk  'BEGIN{min=0}{if(($2)<min)  min=($2)}END {print min}'

Input

Code:
-122.2840   42.0009
-119.9950   41.1777
>
-123.7540   39.5520
-123.7820   39.6872

The above scripts are not outputting the correct values. Ive tried using "-FS" to ignore ">" but i dont think its being used correctly.

Note that finding min on column 1 and max on column 2 does work properly.
Code:
awk  'BEGIN{max=0}{if(($2)>max)  max=($2)}END {print max}' 
awk  'BEGIN{min=0}{if(($1)<min)  min=($1)}END {print min}'

# 2  
Old 07-29-2015
Note that 0 will always be greater than negative values, so $1 will never exceed max in your code snippet. Same for the min values...
Try
Code:
awk  '
BEGIN           {max=-1E100}
NF > 1          {if (($1)>max)  max=($1)
                }
END             {print max}
' file

---------- Post updated at 17:54 ---------- Previous update was at 17:51 ----------

or even
Code:
awk  '
BEGIN           {max=-1E100
                 min=1E100
                }
NF > 1          {if (($1)>max)  max=($1)
                 if (($2)<min)  min=($2)
                }
END             {print max, min}
' file
-119.9950 39.5520

# 3  
Old 07-29-2015
Hello ncwxpanther,

If you want to get maximum and minimum values in both the columns across the whole file then following may help you in same.
Code:
 awk 'BEGIN{col1_max=col2_max=-1E100;col1_min=col2_min=1E100} ($0 !~ />/){col1_max=col1_max > $1?col1_max : $1; col2_max=col2_max > $2?col2_max : $2; col1_min=col1_min < $1?col1_min:$1; col2_min=col2_min < $2?col2_min:$2;} END{print "COL1_max" OFS "COL2_max" OFS "COL1_min" OFS "COL2_min" ORS col1_max OFS col2_max OFS col1_min OFS col2_min}'  Input_file

Output will be as follows.
Code:
 COL1_max COL2_max COL1_min COL2_min
-119.9950 42.0009 -123.7820 39.5520

EDIT: Adding a non-one liner form of solution now for same.
Code:
cat min_max.ksh
awk 'BEGIN{
                col1_max=col2_max=-1E100;
                col1_min=col2_min=1E100
          }
                ($0 !~ />/){
                                        col1_max=col1_max > $1?col1_max : $1;
                                        col2_max=col2_max > $2?col2_max : $2;
                                        col1_min=col1_min < $1?col1_min : $1;
                                        col2_min=col2_min < $2?col2_min : $2;
                           }
                END        {
                                        print "COL1_max" OFS "COL2_max" OFS "COL1_min" OFS "COL2_min" ORS col1_max OFS col2_max OFS col1_min OFS col2_min
                           }
    ' Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-29-2015 at 01:06 PM.. Reason: Added a non-one liner form in solution now
# 4  
Old 07-29-2015
The problem with max is the > and comparing strings to numbers.
The problem with min is we start with min being 0 and none of those are below zero.

Let's skip lines that don't have 2 columns, and also initialize using the first line.

Code:
$ awk  'NR==1 {min=$2; next} NF>1 && $2<min {min=$2} END {print min}' input
39.5520

$ awk  'NR==1 {max=$1; next} NF>1 && $1>max {max=$1} END {print max}' input
-119.9950

This User Gave Thanks to neutronscott For This Post:
# 5  
Old 07-29-2015
Code:
awk 'FNR==1 {max=$1;min=$2;next} NF>1{if($1>max) max=$1; if ($2<min) min=$2} END {print max, min}' myFile

# 6  
Old 07-29-2015
Quote:
Originally Posted by neutronscott

Code:
$ awk  'NR==1 {min=$2; next} NF>1 && $2<min {min=$2} END {print min}' input
39.5520

$ awk  'NR==1 {max=$1; next} NF>1 && $1>max {max=$1} END {print max}' input
-119.9950


Thanks. I got these 2 scripts to print the min and max of each column separately. Whats the best way to combine these into a single line?

Code:
awk 'FNR==1 {min=$1;max=$1;next} NF>1{if($1<min) min=$1; if ($1>max) max=$1} END {print max, min}'
awk 'FNR==1 {min=$2;max=$2;next} NF>1{if($2<min) min=$2; if ($2>max) max=$2} END {print max, min}'

Output
Code:
MinColumn1 MaxColumn1 MinColumn2 MaxColumn2

# 7  
Old 07-29-2015
something along these lines - for any number of fields:
awk -f nc.awk myFile where nc.awk is:
Code:
function mm(fnr,   i)
{
  for(i=1; i<=NF;i++) {
    if (fnr==1) {min[i]=$i;max[i]=$i;continue}
    if ($i<min[i]) min[i]=$i
    if ($i>max[i]) max[i]=$i
  }
}

NF>1 {mm(FNR)}
END {
  for(i=1;i in min;i++) 
     printf("%s%s%s%s", min[i], OFS, max[i], ((i+1) in min)?OFS:ORS)
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk Sort 2d histogram output from min(X,Y) to max(X,Y)

I've got Gnuplot-format 2D histogram data output which looks as follows. 6.5 -1.25 10.2804 6.5404 -1.25 10.4907 6.58081 -1.25 10.8087 6.62121 -1.25 10.4686 6.66162 -1.25 10.506 6.70202 -1.25 10.3084 6.74242 -1.25 9.68256 6.78283 -1.25 9.41229 6.82323 -1.25 9.43078 6.86364 -1.25 9.62408... (1 Reply)
Discussion started by: chrisjorg
1 Replies

2. Shell Programming and Scripting

How to get min and max values using awk?

Hi, I need your kind help to get min and max values from file based on value in $5 . File1 SP12.3 stc 2240806 2240808 + ID1_N003 ID2_N003T0 SP12.3 sto 2241682 2241684 + ID1_N003 ID2_N003T0 SP12.3 XE 2239943 2240011 + ID1_N003 ID2_N003T0 SP12.3 XE 2240077 2241254 + ID1_N003 ... (12 Replies)
Discussion started by: redse171
12 Replies

3. Shell Programming and Scripting

awk script to find min and max value

I need to find the max/min of columns 1 and 2 of a 2 column file what contains the special character ">". I know that this will find the max value of column 1. awk 'BEGIN {max = 0} {if ($1>max) max=$1} END {print max}' input.file But what if I needed to ignore special characters in the... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

4. Shell Programming and Scripting

Get the min avg and max with awk

aaa: 3 ms aaa: 2 ms aaa: 5 ms aaa: 10 ms .......... to get the 3 2 5 10 ...'s min avg and max something like min: 2 ms avg: 5 ms max: 10 ms (2 Replies)
Discussion started by: yanglei_fage
2 Replies

5. Shell Programming and Scripting

Average, min and max in file with header, using awk

Hi, I have a file which looks like this: FID IID MISS_PHENO N_MISS N_GENO F_MISS 12AB43131 12AB43131 N 17774 906341 0.01961 65HJ87451 65HJ87451 N 10149 906341 0.0112 43JJ21345 43JJ21345 N 2826 906341 0.003118I would... (11 Replies)
Discussion started by: kayakj
11 Replies

6. Shell Programming and Scripting

AWK script - extracting min and max values from selected lines

Hi guys! I'm new to scripting and I need to write a script in awk. Here is example of file on which I'm working ATOM 4688 HG1 PRO A 322 18.080 59.680 137.020 1.00 0.00 ATOM 4689 HG2 PRO A 322 18.850 61.220 137.010 1.00 0.00 ATOM 4690 CD ... (18 Replies)
Discussion started by: grincz
18 Replies

7. Shell Programming and Scripting

Find min.max value if matching columns found using AWK

Input_ File : 2 3 4 5 1 1 0 1 2 1 -1 1 2 1 3 1 3 1 4 1 6 5 6 6 6 6 6 7 6 7 6 8 5 8 6 7 Desired output : 2 3 4 5 -1 1 4 1 6 5 6 8 5 8 6 7 (3 Replies)
Discussion started by: vasanth.vadalur
3 Replies

8. Shell Programming and Scripting

Ignoring special character while running a job

I am running a program as follows (using uniface) "chngpasswd.sh drg_ldos1 manager64 manager65 SMART2AP" in which manager64 , manager65 is a variables and keeps on changing, above command works fine but in case we have special characters it fails as shown below "chngpasswd.sh drg_ldos1... (3 Replies)
Discussion started by: lalitpct
3 Replies

9. UNIX for Dummies Questions & Answers

Iterate a min/max awk script over time-series temperature data

I'm trying to iterate a UNIX awk script that returns min/max temperature data for each day from a monthly weather data file (01_weath.dat). The temperature data is held in $5. The temps are reported each minute so each day contains 1440 temperature enteries. The below code has gotten me as far as... (5 Replies)
Discussion started by: jgourley
5 Replies

10. UNIX for Dummies Questions & Answers

Awk search for max and min field values

hi, i have an awk script and I managed to figure out how to search the max value but Im having difficulty in searching for the min field value. BEGIN {FS=","; max=0} NF == 7 {if (max < $6) max = $6;} END { print man, min} where $6 is the column of a field separated by a comma (3 Replies)
Discussion started by: Kirichiko
3 Replies
Login or Register to Ask a Question