Finding Max value from an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding Max value from an array
# 8  
Old 08-02-2009
Thanks for the replies.

But now i have modified my script from an array to a file. Now I will have to find the Max and Second Max from a file and store them in two variables.

Also

---------- Post updated at 09:39 AM ---------- Previous update was at 09:14 AM ----------

[/COLOR]
Quote:
Originally Posted by matrixmadhan
Thanks for posting what you have tried Smilie

One more variable and keep flipping - but not efficient

Code:
awk 'BEGIN{ max = -1; second_max = -1 }{ if ( $0 > max ) { second_max = max; max = $0 } }END{ print second_max }' file

This is failing in the case the contents of file are:
0336
0405
0337
0338

max is 0405 and second max is 0338.. but above code is returning 0336 as second max..

Last edited by vjasai; 08-02-2009 at 11:37 AM..
# 9  
Old 08-02-2009
Here is solution for you, little fix your 1st script. No need to echo array, use it. You can test values or give the list for sort and then take so many value as you like.
Code:
#!/bin/ksh
#!/bin/bash

array=(0338 0337 0339 0340 0401 0402 0403)
for i in ${array[*]}
do
        echo "$i"
done | sort -nr | head -2

Or you can put sorted list to the array and take so many values as you like:
Code:
# set array using array=( list of values )
# make max
max=(
$(for i in ${array[*]}
do
        echo "$i"
done | sort -nr ) 
)
echo "1st: ${max[0]}" 
echo "2nd: ${max[1]}"
echo "3th: ${max[2]}"



---------- Post updated at 08:56 PM ---------- Previous update was at 08:32 PM ----------

Using file:
Code:
array=( $(<somefile ) )

max=(
$(for i in ${array[*]}
do
        echo "$i"
done | sort -nr )
)
echo "1. ${max[0]}"
echo "2. ${max[1]}"

Or using sort and two first lines:
Code:
cnt=1
sort -nr somefile | while read line
do
        (( cnt  > 2 )) && break
        echo "$cnt. $line"
        (( cnt+=1))
done

Or ...

---------- Post updated at 09:00 PM ---------- Previous update was at 08:56 PM ----------

Using sort + awk
Code:
sort -nr somefile | awk 'NR <= 2 { print NR ".",$1 }'

# 10  
Old 08-03-2009
Quote:
Originally Posted by vjasai
Thanks for the replies.

But now i have modified my script from an array to a file. Now I will have to find the Max and Second Max from a file and store them in two variables.

Also

---------- Post updated at 09:39 AM ---------- Previous update was at 09:14 AM ----------

[/COLOR]

This is failing in the case the contents of file are:
0336
0405
0337
0338

max is 0405 and second max is 0338.. but above code is returning 0336 as second max..
That's a good catch Smilie

Code:
awk 'BEGIN{ max = -1; second_max = -1 }{ if ( $0 > max ) { second_max = max; max = $0 } else { if ( $0 > second_max ) second_max = $0 } }END{ print second_max }' filename

# 11  
Old 08-03-2009
Quote:
Originally Posted by scottn
Leading 0's will take care of that...
Was not sure if its always going to be 4 char long
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Php number array from max, min, step size mysql data

I want to create a form with data values in a dropdown list. The values in the dropdown list need to be generated on the fly from max, min and increment values contained in a mysql database. Hopefully this makes sense, I really have no idea where to start :confused: Thanks (6 Replies)
Discussion started by: barrydocks
6 Replies

2. Shell Programming and Scripting

Finding max of a column grouping by the time

Hi, I have the below text: 16:00 0.50 16:00 0.30 16:00 0.00 16:00 0.00 16:00 0.30 16:01 0.00 16:01 0.30 I want to find the max of the 2nd column grouping by the values in the 1st column using awk. So 16:00 0.50 16:01 0.30 I have tried (3 Replies)
Discussion started by: satishrao
3 Replies

3. Shell Programming and Scripting

awk, max value, array, row

Hello: I want to print out the entire row with max value in column 3 based on column 2. Input file is millions rows. test.dat: Contig1 lcl|1DL 111 155 265 27 Contig2 lcl|1DS 100 73 172 100 Contig3 lcl|1DL 140 698 837 140 Contig3 lcl|6DS 107 1488 1594... (1 Reply)
Discussion started by: yifangt
1 Replies

4. Shell Programming and Scripting

Finding max number in filename and opening it

Hi, I have files named as energy.dat.1 energy.dat.2 energy.dat.3 ... energy.dat.2342 I would like to find the file with maximum number in the filename (ex. energy.dat.2342) and open it. Would you please share your expertize in writing the script? Thanks in advance. (8 Replies)
Discussion started by: rpd25
8 Replies

5. Shell Programming and Scripting

finding max size

Hi I have a list of 2000 records with multiple entries and I want to get the max size for each entry ABC 1 ABC 2 ABC 3 ABC 4 DEF 1 DEF 2 DEF 2 DEF 2 DEF 2 ... (9 Replies)
Discussion started by: Diya123
9 Replies

6. HP-UX

max limit in in setting array

hi, iam getting error when i assign a variable to an array of more that 315 character in length set -A array <variable> <variable> value is 000001 000002 and up to 000045 it is giving error as "The specified subscript cannot be greater than 1024." can any one help me to solve this (2 Replies)
Discussion started by: gomathi
2 Replies

7. Shell Programming and Scripting

Max amount of awk array indices

Does anyone know what the max amount of indices you can store in a awk array? (0 Replies)
Discussion started by: timj123
0 Replies

8. Shell Programming and Scripting

Help in finding the max and min position

Hi, I have this input file called ttbitnres (which is catenated and sorted):- 8 0.4444 213 10 0.5555 342 11 0.5555 321 12 0.5555 231 13 0.4444 400 My code is at :- #!/bin/bash echo -e Version "\t" Number of Pass "\t" Number of Fail "\t" Rank Position "\t"Min "\t" Max... (1 Reply)
Discussion started by: ahjiefreak
1 Replies

9. UNIX for Advanced & Expert Users

MAX SIZE ARRAY Can Hold it

Hi, Do anyone know what's the max size of array (in awk) can be store before hit any memory issue. Regards (3 Replies)
Discussion started by: epall
3 Replies

10. Shell Programming and Scripting

Finding max value

My code below is supposed to find which company had the most business and then print the appropriate fields from another file which are the companies ID number and name. I can loop through awk and display all the total amount of business for each company but I need help in only printing out the... (1 Reply)
Discussion started by: Enigma23
1 Replies
Login or Register to Ask a Question