Using awk to find max and printing entire line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using awk to find max and printing entire line
# 1  
Old 10-07-2013
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:
Code:
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 I would want it to find that 5.26 is the max and then print the entire line to look like this:
Code:
5.26|2000-02-03 11:15:00

This is what I have currently, but instead of printing column 2 that is associated with the max, it prints the very last value for column2 in the file:
Code:
max_stg=$(awk 'max=="" || $1 > max {max=$1} END{ print max "|" $2}' FS="|" $STAGE_FILE)

So, the above results in:
Code:
5.26|2000-02-01 18:30:00

Any help would be greatly appreciated. Thanks so much!

Last edited by Franklin52; 10-08-2013 at 03:06 AM.. Reason: Please use code tags
# 2  
Old 10-07-2013
So just change slightly what you have...
Code:
$(awk 'max=="" || $1 > max {max=$1; l=$0} END{ print l}' FS="|" $STAGE_FILE)

# 3  
Old 10-07-2013
Thank you, thank you, thank you, shamrock! You rock! It works great.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Avoid printing entire line if string not found

so im searching the process table with: ps -ef | awk -F"./rello.java" '{ print substr($0, index($0,$2)) }' I only want it to print everything that's infront of the "./rello.java". That's because im basically getting the arguments that was passed to the rello.java script. this works. ... (2 Replies)
Discussion started by: SkySmart
2 Replies

2. Shell Programming and Scripting

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 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 ... (8 Replies)
Discussion started by: ncwxpanther
8 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

awk to find the max

Experts, Here is my question. I have got file like below # cat file XYZb,24,26,6 XYZc,24,26,6 XYZe,24,25,5 XYZf,23,29,5 XYZi,16,25,5 XYZj,24,26,7 XYZn,17,23,4 XYZz,23,29,5 Now, I want to print the line's Column1 whose Column 3 is the max of all. My code is awk -F',' 'BEGIN {max... (9 Replies)
Discussion started by: sathyaonnuix
9 Replies

5. Shell Programming and Scripting

awk if statement not printing entire field

I have an input that looks like this: chr1 mm9_knownGene utr3 3204563 3206102 0 - . gene_id "Xkr4"; transcript_id "uc007aeu.1"; chr1 mm9_knownGene utr3 4280927 4283061 0 - . gene_id "Rp1"; transcript_id "uc007aew.1"; chr1 mm9_knownGene ... (5 Replies)
Discussion started by: pbluescript
5 Replies

6. 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

7. Shell Programming and Scripting

Printing entire field, if at least one row is matching by AWK

Dear all, I have been trying to print an entire field, if the first line of the field is matching. For example, my input looks something like this. aaa ddd zzz 123 987 126 24 0.650 985 354 9864 0.32 0.333 4324 000 I am looking for a pattern,... (5 Replies)
Discussion started by: Chulamakuri
5 Replies

8. Shell Programming and Scripting

grep to find entire line ....

As per my understanding below mentioned line of code finding a word 'boy' in $ACULOG... num_errors=`grep -i -e fail -e illegal -e exception -e "<E" -e boy $ACULOG | wc -l` if I'm not corerct, please correct me. How I can find entire line like "This is a boy" with something similar as above... (1 Reply)
Discussion started by: heyitsmeok
1 Replies

9. 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

10. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies
Login or Register to Ask a Question