Find Max value of line and print


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find Max value of line and print
# 1  
Old 02-10-2016
Find Max value of line and print

I need to find the max value of all columns except the 1st column and print the answer along with the 1st column.

Input

Code:
123xyz   0    0    1    2    0    0    0    0    0    0    0     
234xyz   0    0    0    0    0    0    0    0    0    0    0    
345xyz   0    0    1    0    0    0    0    0   -9   -9   -9

Expected Output

Code:
123xyz   2

I have this code to look for all values in entire file, but it does not ignore the 1st column.
Code:
awk '{for(x=1;x<=NF;x++)a[++y]=$x}END{c=asort(a);print "max:",a[c]}'

# 2  
Old 02-10-2016
Why don't you start at x=2 if you're not interested in $1?
However, try
Code:
awk '
BEGIN   {MAX=-1E100
        }
        {for (x=2; x<=NF; x++) if ($x>MAX)      {MAX = $x
                                                 C1  = $1
                                                }
        }
END     {print C1, MAX
        }
' file
123xyz 2

This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-10-2016
The original code can be extended like this
Code:
awk '{ for(x=2;x<=NF;x++) {a[++y]=$x; b[$x]=$1} } END { c=asort(a); print "max:",b[a[c]],a[c] }' file

But is more efficient to only remember the current maximum and its field1, in two variables. Like RudiC did.
If you do not like the initialization MAX=-1E100, then you can do
Code:
awk '
NR==1 {MAX=$2; C1=$1}
{ for (x=2; x<=NF; x++) if ($x>MAX) { MAX=$x; C1=$1 } }
END { print C1, MAX } 
' file

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 02-10-2016
Thanks. I got both of these to work.

My first column contains a lot of unnecessary information. Is there a way to space out the fields 1-11, 13-16, 18-19 and 20-27?

Input
Code:
USW00003812Y2016M02TMAXLE32 0    0    1    2    0    0    0    0    0    0    0

Desired output
Code:
USW00003812 201602 TMAXLE32 2

# 5  
Old 02-10-2016
Instead of
Code:
{ print C1, MAX }

You can do
Code:
{ print substr(C1,1,11), substr(C1,13,4), substr(C1,18,2), substr(C1,20,8)}, MAX }

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 02-11-2016
I have the following code set up to analyze several thousand files is multiple directories. The result output is only a single max out of all $file per $dir.

I am expecting a max value for each of the $file for each $dir to be returned.

What am I missing?


Code:
#!/bin/bash

for dir in a b c x y z
do

for file in USW*
do
awk '
BEGIN   {MAX=-1E100
        }
        {for (x=2; x<=NF; x++) if ($x>MAX)      {MAX = $x
                                                 C1  = $1
                                                }
        }
END     {print substr(C1,1,11), substr(C1,13,4), substr(C1,18,2), substr(C1,20,8), MAX
        }
' /dir/of/files/$dir/$file
done

done

# 7  
Old 02-11-2016
Quote:
Originally Posted by ncwxpanther
.
.
.
I am expecting a max value for each of the $file for each $dir to be returned.
.
.
.
It should return one result line per file. Please show the directory/file structure and/or what your two for loops make of it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find line then evaluate text on next line, print when condition is met

Hello, I am looking for a specific situation in a text file. The conditions are, > <CompoundName> InChI=1S/C5H12NO2/c1-5(2)4-8-6(3)7/h5H,4H2,1-3H3/q+1 I am looking for cases where the line "> <CompoundName>" is followed by a line that contains the string "InChI=" without regard to... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. UNIX for Dummies Questions & Answers

Using awk to find max and printing entire line

Hi folks, I am very new to awk. I have what is probably a very simple question. I'm trying to get the max value of column 1, but also print column 2. My data looks like this: 0.044|2000-02-03 14:00:00 5.23|2000-02-03 05:45:00 5.26|2000-02-03 11:15:00 0|2000-02-01 18:30:00 So in this case... (2 Replies)
Discussion started by: amandarobe
2 Replies

3. Shell Programming and Scripting

Find 2 expressions then print in a single line

Guys, need help. I have a file that contains something like this: abc def ghi jkl I want to print the first and last line of the file and the output should be in a single line. so, output should be like this: abc jkl (3 Replies)
Discussion started by: solidhelix08
3 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Print a line using a max and a min values of different columns

Hi guys, I already search on the forum but i can't solve this on my own. I have a lot of files like this: And i need to print the line with the maximum value in last column but if the value is the same (2 in this exemple for the 3 last lines) i need get the line with the minimum value in... (4 Replies)
Discussion started by: MetaBolic0
4 Replies

5. UNIX for Dummies Questions & Answers

loop? print max column in each line for 800 files and merge

Hello, I have 800 or so files with 3 columns each and >10000 lines each. For each file and each line I would like to print the maximum column number for each line. Then I would like to 'paste' each of these files together (column-wise) so that the file with expression in label '_1' is the... (6 Replies)
Discussion started by: peanuts48
6 Replies

6. Shell Programming and Scripting

Find a string using grep & print the line above or below that.

Hi All, Please tell me how can I Find a string using grep & print the line above or below that in solaris? Please share as I am unable to use grep -A or grep -B as it is not working on Solaris. (10 Replies)
Discussion started by: Zaib
10 Replies

7. Shell Programming and Scripting

awk find a string, print the line 2 lines below it

I am parsing a nagios config, searching for a string, and then printing the line 2 lines later (the "members" string). Here's the data: define hostgroup{ hostgroup_name chat-dev alias chat-dev members thisisahostname } define hostgroup{ ... (1 Reply)
Discussion started by: mglenney
1 Replies

8. Shell Programming and Scripting

How to find and print the last word of each line from a text file

Can any one help us in finding the the last word of each line from a text file and print it. eg: 1st --> aaa bbbb cccc dddd eeee ffff ee 2nd --> aab ered er fdf ere ww ww f the o/p should be a below. ee f (1 Reply)
Discussion started by: naveen_sangam
1 Replies

9. Shell Programming and Scripting

Perl : Find a string and Print full line

Hi Need a perl script to read lines in a file, scan for a string named "APPLE" and write to different file the only lines containing the matched string. (5 Replies)
Discussion started by: PrasannaKS
5 Replies

10. 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
Login or Register to Ask a Question