awk second largest, third largest value

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk second largest, third largest value
# 8  
Old 09-14-2011
i'm having a hard time understanding the !second{print $0;second=$2;}$2<second{print $0;exit;}' line

I want to understand it to tailor it to find 3rd largest maxes with difference greater than 5 (as used in second largest logic), 4th largsest maxes with that same difference, 5th largest, etc. etc.

Thanks for the help so far. I am in unfamiliar territories with awk here.
# 9  
Old 09-14-2011
hi, see this example:

say max=100

if you want the 2nd largest number is 80

, then the next smaller should be the 3rd largest (79)

and you don't have to compare 3rd and max, to check if the difference is > 5.

or you want to ensure that difference between 2nd and 3rd > 5?

---------- Post updated at 17:16 ---------- Previous update was at 17:05 ----------

regarding this line:
 !second{print $0;second=$2;}$2<second{print $0;exit;}'

if a variable is not assigned any value, it returns false in a boolean check. so "!second" means "if variable <second> has not been set, then do {block}"
# 10  
Old 09-14-2011
yes, i want to ensure between 2nd and 3rd is > 5, and 3rd and 4th is > 5, 4th and 5th > 5, etc. etc.

# 11  
Old 09-14-2011
don't say etc. etc. how many "largest" numbers do you want?
2nd -- ?th
or all as long as possible?
# 12  
Old 09-14-2011
up to 8 largest, with the differences still there (i.e. difference greater than 5 between the previous largest).

thanks for helping me out, I've never gone this far in awk before. But I am learning.
# 13  
Old 09-14-2011
here I made an example, hope that it helps:

a sorted file (descending) named t

kent$  cat t
foo 100
foo 99
foo 98
foo 97
foo 96
foo 95
foo 94
foo 93
foo 92
foo 5
foo 4
foo 3
foo 2
foo 1

awk one liner gives you 8 largest number with 5 as difference with previous :

kent$  awk 'NR==1{max=$2;next;}max-$2>5{print $0;max=$2;c++;}c==8{exit}' t
foo 94
foo 88
foo 82
foo 76
foo 70
foo 64
foo 58
foo 52

This User Gave Thanks to sk1418 For This Post:
# 14  
Old 09-14-2011
thanks this is perfect! thanks for stickin it out with me!
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 combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Largest number in array. Help!

I need to calculate the biggest number in array size n. Example: Users enter: 1 7 4 9 The biggest number is : 9 Simple but I'm really new on this on Shell/Bash! Anything will be helpful! Thanks! #!/bin/bash printf "\tEnter a list of numbers, with spaces: " read -a ARRAY BIG=$1... (5 Replies)
Discussion started by: Sundown
5 Replies

3. UNIX for Dummies Questions & Answers

Printf awk: argument to adjust automatically width to the largest value

Hi ! Here is an example of a tab-delimited file with variable width values in field 2 (space separated values). As the real file contains lot of lines, I can adjust manually the width to the largest value in field 2. Does it exist an argument to do it automatically? (4 Replies)
Discussion started by: lucasvs
4 Replies

4. Shell Programming and Scripting

Help with keep the largest record in file

Input file US Score 10 UK Ball 20 AS Score 50 AK Ball 10 PZ Ballon 50 PA Score 70 WT Data 10 . . Desired output file UK Ball 20 PZ Ballon 50 PA Score 70 WT Data 10 . . (1 Reply)
Discussion started by: perl_beginner
1 Replies

5. Shell Programming and Scripting

AWK (how) to get smallest/largest nr of ls -la

Hey, This is a long-shot however, I am stuck with the following problem: I have the output from ls -la, and I want to sort some of that data out by using AWK to filter it. ls -la | awk -f scriptname.awk Input: For example: drwxr-xr-x 3 user users 4096 2010-03-14 20:15 bin/... (5 Replies)
Discussion started by: abciscool
5 Replies

6. UNIX for Dummies Questions & Answers

largest file

My directory only contains sub directories..I want to know about the largest file in directory..Largest in terms of size..And what if I want to know about first 3 largest files.. Is it possible to know largest file in terms of its filename?? (4 Replies)
Discussion started by: aadi_uni
4 Replies

7. Shell Programming and Scripting

Find largest files

Hello, i am on linux 2.6.13-1.1526_FC4smp , and i would be very greatfull if someone could help to find the largest files on that server. I know i can find files with find command or list them with ls , but is there a way that i could list let's say 10 biggest files on server ? :o (11 Replies)
Discussion started by: tonijel
11 Replies

8. Shell Programming and Scripting

largest field , awk , help

Hi All, My file is like this: $ cat max.txt abcd:1982:a efghij:1980:e klmn:1923:k opqrst:1982:o I have to find out the largest first field and the corresponding line. i.e Output required: efghij efghij:1980:e opqrst opqrst:1982:o HTH, jkl_jkl (6 Replies)
Discussion started by: jkl_jkl
6 Replies

9. Shell Programming and Scripting

largest value from within a file

hi, i am having a file that looks like this : myhome111 oper 11 myhome333 oper 19 ... how can i get the largest value from this file this example i am consiering 19 as the largest value.. how can i do that.. as this file is having other numbers also within it like 111, 333... (4 Replies)
Discussion started by: kripssmart
4 Replies

10. Shell Programming and Scripting

find largest file

Hi, 1)I have XX directory and have lot of files ,I want to find largest file in that directory 2)how calculate the size of file in MB. Thanks, Mohan (15 Replies)
Discussion started by: mohan705
15 Replies
Login or Register to Ask a Question