Sponsored Content
Top Forums Shell Programming and Scripting Get the MAX value out of a column Post 302798617 by alister on Thursday 25th of April 2013 12:33:42 AM
Old 04-25-2013
While the OP's sample data consists exclusively of positive integers, if negative values and zeroes must be handled, neither solution is correct.

Corona's code will unconditionally set MAX to the next line's $3, if MAX is ever set to 0.
Code:
$ cat file
-1 -1 -1 -1
0 0 0 0
-2 -2 -2 -2
$ awk 'NR==FNR { if((!MAX)||(MAX<$3)) MAX=$3; next }; $3==MAX' file file
-2 -2 -2 -2
$ awk 'NR==FNR { if((NR==1)||(MAX<$3)) MAX=$3; next }; $3==MAX' file file
0 0 0 0

Yoda's code implicitly uses a max of 0 when NR==1 (a numeric 0 is the default value of an uninitialized variable). If that implicit 0, which is not present in the file, is greater than all values, B[2]==max will never succeed and no output is generated. max must be initialized from the first value of $3.
Code:
$ cat file
-1 -1 -1 -1
-2 -2 -2 -2
-3 -3 -3 -3
$ awk '{A[NR" "$3]=$0;max=max<$3?$3:max}END{for(k in A){split(k,B);if(B[2]==max) print A[k]}}' f
$ awk '{A[NR" "$3]=$0;max=max<$3||NR==1?$3:max}END{for(k in A){split(k,B);if(B[2]==max) print A[k]}}' f
-1 -1 -1 -1

Regards,
Alister
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Max column count in a file

I have to send a file to mainframe and before sending it, I have to execute the quote command to set the record length. Since the file is dynamic, I do not know what the maximum size of a line could be. Currently, I use the following function to get the Max Column Count. Since I use "sed" it... (2 Replies)
Discussion started by: gemini
2 Replies

2. Shell Programming and Scripting

Help: Find line where column has max vlaue

Hi all, Ive searched the forum but with no luck... I have a file: ID Name Val 1 bob 45 2 joe 89 3 sue 11 4 steve 89 ... etc I want to find a way to print... (6 Replies)
Discussion started by: muay_tb
6 Replies

3. Shell Programming and Scripting

Calc max of a column

In C that was easy with a for and if. Iam trying to learn a litle more in bash. Example Ronaldo:5800 Figo:4000 Rafael:2321 Kaka:1230 I want the max of the $2 and the output will be: The max value is 5800 from Ronaldo. How can i do this in shell? Thanks for all, folks. (11 Replies)
Discussion started by: rafazz
11 Replies

4. Shell Programming and Scripting

loop in awk - column max for each column

Hello all, this should really be easy for you... I need AWK to print column maxima for each column of such input: Input: 1 2 3 1 2 1 1 3 2 1 1 2 Output should be: 2 2 3 3 This does the sum, but i need max instead: { for(i=1; i<=NF; i++) sum +=$i } END {for(i=1; i in sum;... (3 Replies)
Discussion started by: irrevocabile
3 Replies

5. Shell Programming and Scripting

Join and awk max column

Hi Friends, I have a file1 with 3400 records that are tab separated and I have a file2 with 6220 records. I want to merge both these files. I tried using join file1 and file2 after sorting. But, the records should be (3400*6220 = 21148000). Instead, I get only around 11133567. Is there anything... (13 Replies)
Discussion started by: jacobs.smith
13 Replies

6. Shell Programming and Scripting

to find min and max value for each column!

Hello Experts, I have got a txt files which has multiple columns, I want to get the max, min and diff (max-min) for each column in the same txt file. Example: cat file.txt a 1 4 b 2 5 c 3 6 I want ouput like: cat file.txt a 1 4 b 2 5 c 3 6 Max 3 6 Min 1 4 Diff 2 2 awk 'min=="" ||... (4 Replies)
Discussion started by: dixits
4 Replies

7. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

8. UNIX for Dummies Questions & Answers

get max value every 4 rows between 2 column

Hi all I have a file that has two columns and I need the maximum value in column 2 of 4 positions o rows. for example at position {1..3} there are 4 characters (A, C, G and T) each of these characters with a value with a value in column 2. I need the maximum value in column 2 and the corresponding... (2 Replies)
Discussion started by: xinox
2 Replies

9. Shell Programming and Scripting

Print min and max value from two column

Dear All, I have data like this, input: 1254 10125 1254 10126 1254 10127 1254 10128 1254 10129 1255 10130 1255 10131 1255 10132 1255 10133 1256 10134 1256 10135 1256 10137... (3 Replies)
Discussion started by: aksin
3 Replies

10. Shell Programming and Scripting

Get min and max value in column

Gents, I have a big file file like this. 5100010002 5100010004 5100010006 5100010008 5100010010 5100010012 5102010002 5102010004 5102010006 5102010008 5102010010 5102010012 The file is sorted and I would like to find the min and max value, taking in the consideration key1... (3 Replies)
Discussion started by: jiam912
3 Replies
cgelsd.f(3)							      LAPACK							       cgelsd.f(3)

NAME
cgelsd.f - SYNOPSIS
Functions/Subroutines subroutine cgelsd (M, N, NRHS, A, LDA, B, LDB, S, RCOND, RANK, WORK, LWORK, RWORK, IWORK, INFO) CGELSD computes the minimum-norm solution to a linear least squares problem for GE matrices Function/Subroutine Documentation subroutine cgelsd (integerM, integerN, integerNRHS, complex, dimension( lda, * )A, integerLDA, complex, dimension( ldb, * )B, integerLDB, real, dimension( * )S, realRCOND, integerRANK, complex, dimension( * )WORK, integerLWORK, real, dimension( * )RWORK, integer, dimension( * )IWORK, integerINFO) CGELSD computes the minimum-norm solution to a linear least squares problem for GE matrices Purpose: CGELSD computes the minimum-norm solution to a real linear least squares problem: minimize 2-norm(| b - A*x |) using the singular value decomposition (SVD) of A. A is an M-by-N matrix which may be rank-deficient. Several right hand side vectors b and solution vectors x can be handled in a single call; they are stored as the columns of the M-by-NRHS right hand side matrix B and the N-by-NRHS solution matrix X. The problem is solved in three steps: (1) Reduce the coefficient matrix A to bidiagonal form with Householder tranformations, reducing the original problem into a "bidiagonal least squares problem" (BLS) (2) Solve the BLS using a divide and conquer approach. (3) Apply back all the Householder tranformations to solve the original least squares problem. The effective rank of A is determined by treating as zero those singular values which are less than RCOND times the largest singular value. The divide and conquer algorithm makes very mild assumptions about floating point arithmetic. It will work on machines with a guard digit in add/subtract, or on those binary machines without guard digits which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or Cray-2. It could conceivably fail on hexadecimal or decimal machines without guard digits, but we know of none. Parameters: M M is INTEGER The number of rows of the matrix A. M >= 0. N N is INTEGER The number of columns of the matrix A. N >= 0. NRHS NRHS is INTEGER The number of right hand sides, i.e., the number of columns of the matrices B and X. NRHS >= 0. A A is COMPLEX array, dimension (LDA,N) On entry, the M-by-N matrix A. On exit, A has been destroyed. LDA LDA is INTEGER The leading dimension of the array A. LDA >= max(1,M). B B is COMPLEX array, dimension (LDB,NRHS) On entry, the M-by-NRHS right hand side matrix B. On exit, B is overwritten by the N-by-NRHS solution matrix X. If m >= n and RANK = n, the residual sum-of-squares for the solution in the i-th column is given by the sum of squares of the modulus of elements n+1:m in that column. LDB LDB is INTEGER The leading dimension of the array B. LDB >= max(1,M,N). S S is REAL array, dimension (min(M,N)) The singular values of A in decreasing order. The condition number of A in the 2-norm = S(1)/S(min(m,n)). RCOND RCOND is REAL RCOND is used to determine the effective rank of A. Singular values S(i) <= RCOND*S(1) are treated as zero. If RCOND < 0, machine precision is used instead. RANK RANK is INTEGER The effective rank of A, i.e., the number of singular values which are greater than RCOND*S(1). WORK WORK is COMPLEX array, dimension (MAX(1,LWORK)) On exit, if INFO = 0, WORK(1) returns the optimal LWORK. LWORK LWORK is INTEGER The dimension of the array WORK. LWORK must be at least 1. The exact minimum amount of workspace needed depends on M, N and NRHS. As long as LWORK is at least 2 * N + N * NRHS if M is greater than or equal to N or 2 * M + M * NRHS if M is less than N, the code will execute correctly. For good performance, LWORK should generally be larger. If LWORK = -1, then a workspace query is assumed; the routine only calculates the optimal size of the array WORK and the minimum sizes of the arrays RWORK and IWORK, and returns these values as the first entries of the WORK, RWORK and IWORK arrays, and no error message related to LWORK is issued by XERBLA. RWORK RWORK is REAL array, dimension (MAX(1,LRWORK)) LRWORK >= 10*N + 2*N*SMLSIZ + 8*N*NLVL + 3*SMLSIZ*NRHS + MAX( (SMLSIZ+1)**2, N*(1+NRHS) + 2*NRHS ) if M is greater than or equal to N or 10*M + 2*M*SMLSIZ + 8*M*NLVL + 3*SMLSIZ*NRHS + MAX( (SMLSIZ+1)**2, N*(1+NRHS) + 2*NRHS ) if M is less than N, the code will execute correctly. SMLSIZ is returned by ILAENV and is equal to the maximum size of the subproblems at the bottom of the computation tree (usually about 25), and NLVL = MAX( 0, INT( LOG_2( MIN( M,N )/(SMLSIZ+1) ) ) + 1 ) On exit, if INFO = 0, RWORK(1) returns the minimum LRWORK. IWORK IWORK is INTEGER array, dimension (MAX(1,LIWORK)) LIWORK >= max(1, 3*MINMN*NLVL + 11*MINMN), where MINMN = MIN( M,N ). On exit, if INFO = 0, IWORK(1) returns the minimum LIWORK. INFO INFO is INTEGER = 0: successful exit < 0: if INFO = -i, the i-th argument had an illegal value. > 0: the algorithm for computing the SVD failed to converge; if INFO = i, i off-diagonal elements of an intermediate bidiagonal form did not converge to zero. Author: Univ. of Tennessee Univ. of California Berkeley Univ. of Colorado Denver NAG Ltd. Date: November 2011 Contributors: Ming Gu and Ren-Cang Li, Computer Science Division, University of California at Berkeley, USA Osni Marques, LBNL/NERSC, USA Definition at line 225 of file cgelsd.f. Author Generated automatically by Doxygen for LAPACK from the source code. Version 3.4.2 Tue Sep 25 2012 cgelsd.f(3)
All times are GMT -4. The time now is 06:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy