Find the maximum of a value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find the maximum of a value
# 1  
Old 11-15-2012
Find the maximum of a value

Code:
0.01 0.6 0.39
0.4 0.3 0.3
1 0 0 
0 0 1

I would like to print 2 if the maximum of a row is the first column
I would like to print 1 if the maximum of a row is the second colum
print 0 if it is the third.

so in this case,
0.6 is the max of the first row so i would want to print 1
for second row i would want to print 2
so the final output will be

Code:
 
1
2
2
0

Thanks!

Last edited by johnkim0806; 11-15-2012 at 02:41 PM..
# 2  
Old 11-15-2012
Your sample output does not fit to your requirement/input sample.
# 3  
Old 11-15-2012
it is fixed now
# 4  
Old 11-15-2012
try:
Code:
awk '{m=NF; b=$1; for (i=1; i<=NF; i++) {if($i>=b) {m--;b=$i;}} print m;}' infile

# 5  
Old 11-16-2012
Code:
$ awk '$1>$2&&$1>$3{o=2}$2>$1&&$2>$3{o=1}$3>$1&&$3>$2{o=0}{print o}' input.txt
1
2
2
0

# 6  
Old 11-16-2012
Hi.

Another alternative:
Code:
#!/usr/bin/awk -f

# @(#) a1	Demonstrate finding maximum of triplet.

# For each line, do 2 comparisons, up to 5 stores.
{ max = $1 ; key = 2
  if ( $2 > max ) { max = $2 ; key = 1 }
  if ( $3 > max ) { key = 0 }
  print key
}

producing:
Code:
% ./a1 data1 
1
2
2
0

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find maximum and minimum from column and store in other column

Need your support for below. Please help to get required output If column 5 is INV then only consider column1 and take out duplicates/identical rows/values from column1 and then put minimum value of column6 in column7 and put maximum value in column 8 and then need to do subtract values of... (7 Replies)
Discussion started by: as7951
7 Replies

2. Shell Programming and Scripting

Find minimum and maximum values based on column with associative array

Hello, I need to find out the minimum and maximum values based on specific column, and then print out the entire row with the max value. Infile.txt: scf6 290173 290416 . + X_047241 T_00113118-1 scf6 290491 290957 . + X_047241 T_00113118-2 scf6 290898 290957 . + X_047241 T_00113119-3 scf6... (2 Replies)
Discussion started by: yifangt
2 Replies

3. UNIX for Beginners Questions & Answers

How to find out the maximum cumulative value?

I have a file has thousands of rows and each row has a number and the number can be positive or negative. I want to do the cumulative sum from the first row. How can I find out the maximum cumulative value when I do the sum work row by row. Here is the example: 4 -3 2 -3 -1 1In this case, the... (5 Replies)
Discussion started by: yuejian
5 Replies

4. Shell Programming and Scripting

Find the maximum value and take value from the neigbouring cells

Let say I have this table A B 0.30 C D 0.60 E F 0.80 G H 0.11 I J 0.10 K L 0.23 M N 0.50 O P 0.01 I need to find the maximum value in each row and output the highest value plus the information two columns back. For example: the output should look like this E F 0.80 M N 0.50 Thanks in... (2 Replies)
Discussion started by: seiksoon
2 Replies

5. UNIX for Dummies Questions & Answers

Using awk to find and use the maximum value in column of data

Dear Unix Gurus, I have a text file with multiple columns, for example, see sample.txt below 0 1 301 1 4 250 2 6 140 3 2 610 7 1 180I want to find the maximum in, say, column 3, normalise all the values to this maximum value (to 4 decimal places) and spit everything into a new... (2 Replies)
Discussion started by: tintin72
2 Replies

6. Homework & Coursework Questions

Find the Maximum value and average of a column

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am trying to complete a script which will allow me to find: a) reads a value from the keyboard. (ask the... (4 Replies)
Discussion started by: dstewie
4 Replies

7. UNIX for Dummies Questions & Answers

Please help me to find out maximum value of a field based on grouping of other fields.

Please help me to find out maximum value of a field based on grouping of other fields, as we do in SQL. Like in SQL if we are having below records : Client_Name Associate_Name Date1 Value C1111 A1111 2012-01-17 10 C1111 A1111 ... (1 Reply)
Discussion started by: KamalKumarKalra
1 Replies

8. Shell Programming and Scripting

TO find the word which occurs maximum number of times

Hi Folks !!!!!!!!!!!!!!!!!!! My Requirement is............. i have a input file: 501,501.chan 502,502.anand 503,503.biji 504,504.raja 505,505.chan 506,506.anand 507,507.chan and my o/p should be chan->3 i.e. the word which occurs maximum number of times in a file should be... (5 Replies)
Discussion started by: aajan
5 Replies

9. UNIX for Dummies Questions & Answers

Find out the maximum growing file in a mount

I need to find the file that is growing in the mount. Say yesterday the utilised space was 95% but today that is 96%. How do i find the file that is growing in size. Have checked the same with du/df options but was not able to find much. Please suggest the best possible option. (3 Replies)
Discussion started by: raman1605
3 Replies

10. UNIX for Dummies Questions & Answers

How to find the maximum # of PIDs

Is there a command in HP Unix which can be used inside a K shell to find out the maximum number of processes (PIDs) a pc can generate? Any help will be greatly appreciated. Steve (8 Replies)
Discussion started by: stevefox
8 Replies
Login or Register to Ask a Question