Sort from highest to lowest number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort from highest to lowest number
# 1  
Old 11-18-2015
Sort from highest to lowest number

Hi Guys,

I am looking for a way to sort the output below from the "Inuse" count from Highest to Lowest. Is it possible?

Thanks in advance.

Code:
user1                                0.12     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user2                                 2.87     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user3                                0.17     0.06        0     0.16
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user4                                0.12     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user5                                 0.13     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user6                                 0.13     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user7                                0.11     0.06        0     0.11
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user8                                0.21     0.06        0     0.21
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user9                                0.75     0.13        0     0.61
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user10                               31.7     0.08        0     31.0
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user11                                0.12     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB

# 2  
Old 11-18-2015
This would work except for the missing separator line of the last record...
Code:
paste -sd"\t\t\t\n" file1 | LC_ALL=C sort -k2,2gr | tr '\t' '\n'
user10                               31.7     0.08        0     31.0
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user2                                 2.87     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user9                                0.75     0.13        0     0.61
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user8                                0.21     0.06        0     0.21
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user3                                0.17     0.06        0     0.16
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user5                                 0.13     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user6                                 0.13     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user1                                0.12     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user11                                0.12     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
user4                                0.12     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user7                                0.11     0.06        0     0.11
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================

This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-18-2015
Thanks!!

But i had errors running this one line script. Btw, my server is aix 7.1.
# 4  
Old 11-18-2015
awk

Code:
awk 'BEGIN{
FS="[ \t]+"
}
{
        if(NR%4==1){
                user=$1
                users[user]=$0
                sort[NR]=$2" "user
        }
        else{
                users[user]=users[user]"\n"$0
        }
}
END{
        n=asort(sort)
        for(i=1;i<=n;i++){
                split(sort[i],items," ")
                user = items[2]
                print(users[user])
        }

}' a

# 5  
Old 11-19-2015
Hi, jaapar.

Writing i had errors running this one line script is not useful, because we cannot be expected to guess what the error is. Not everyone has access to an AIX system. I guessed that the AIX sort does not accept everything that GNU sort accepts. Here is what I found:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate sort group of lines.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C paste sort tr

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
# paste -sd"\t\t\t\n" file1 | LC_ALL=C sort -k2,2gr | tr '\t' '\n'
# paste -sd"\t\t\t\n" data1 | LC_ALL=C sort -k2,2gr | tr '\t' '\n'
paste -sd"\t\t\t\n" data1 | LC_ALL=C sort -k2,2nr | tr '\t' '\n'

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
aix 7.1.0.0
bash GNU bash 4.2.10
paste - ( /usr/bin/paste, Aug 8 2010 )
sort - ( /usr/bin/sort, Sep 5 2012 )
tr - ( /usr/bin/tr, Aug 8 2010 )

-----
 Input data file data1:
user1                                0.12     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user2                                 2.87     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user3                                0.17     0.06        0     0.16
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user4                                0.12     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user5                                 0.13     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user6                                 0.13     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user7                                0.11     0.06        0     0.11
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user8                                0.21     0.06        0     0.21
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user9                                0.75     0.13        0     0.61
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user10                               31.7     0.08        0     31.0
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user11                                0.12     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB

-----
 Results:
user10                               31.7     0.08        0     31.0
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user2                                 2.87     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user9                                0.75     0.13        0     0.61
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user8                                0.21     0.06        0     0.21
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user3                                0.17     0.06        0     0.16
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user5                                 0.13     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user6                                 0.13     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user1                                0.12     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user11                                0.12     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
user4                                0.12     0.06        0     0.12
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================
user7                                0.11     0.06        0     0.11
User                                 Inuse      Pin     Pgsp  Virtual
Unit: GB
===============================================================================

So a simple change in the sort options seems to work with the code from RudiC.

Help us help yourself -- in the future give us the error message.

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Awk, highest and lowest value of a column

Hi again! I am still impressed how fast I get a solution for my topic "average specific column value awk" yesterday. The associative arrays in awk work fine for me! But now I have another question for the same project. Now I have a list like this 1 -0.1 1 0 1 0.1 2 0 2 0.2 2 -0.2 How... (10 Replies)
Discussion started by: bjoern456
10 Replies

2. Shell Programming and Scripting

Perl: find next available lowest number that is available in two arrays

Hi there. I have a number allocation problem whereby I have 2 arrays built from 2 different sources. The arrays will just contain a listed of sorted numbers @a 1 7 10 14 15 16 @b 1 7 10 11 14 15 16 (2 Replies)
Discussion started by: hcclnoodles
2 Replies

3. Shell Programming and Scripting

top 10 highest and lowest percentile from a column

Hi, I want to extract the the top 10 and lowest 10 percentile for a column of values. For example in column 2 for this file: JOE 1 JAY 5 JAM 6 JIL 8 JIB 4 JIH 3 JIG 2 JIT 7 JAM 9 MAR 10 The top 10 lowest will be: JOE 1 and the top 10 highest will be: (2 Replies)
Discussion started by: kylle345
2 Replies

4. Shell Programming and Scripting

Selecting lowest and highest values in columns 1 and 2, based on subsets in column 3

Hi, I have a file with the following columns: 361459 447394 CHL1 290282 290282 CHL1 361459 447394 CHL1 361459 447394 CHL1 178352861 178363529 AGA 178352861 178363529 AGA 178363657 178363657 AGA Essentially, using CHL1 as an example. For any line that has CHL1 in... (2 Replies)
Discussion started by: hubleo
2 Replies

5. Shell Programming and Scripting

ksh program that finds the lowest number in a .txt file

i am having a problem finding the lowest number after punching in a bunch of numbers in the .txt file but its probably the way i have the code set up. help please! (4 Replies)
Discussion started by: tinsteer
4 Replies

6. Shell Programming and Scripting

print every 20 lines the lowest number

Hello all, How can I find the lowest number every 10 lines? For example i have a list name1 -0.1 name2 2 name3 3 name4 -3 name5 1 name6 2 name7 34 name8 34 (6 Replies)
Discussion started by: TheTransporter
6 Replies

7. Shell Programming and Scripting

Extract the highest number out

Hi Gurus, I've using HPUX B.11.23 U ia64 with shell = sh. I've been having some problem get the highest number of this script. Actually I wanted to get the highest number from this listing (TEST123 data and based on this highest number, there will be email being sent out. For example,... (6 Replies)
Discussion started by: superHonda123
6 Replies

8. Shell Programming and Scripting

Korn Shell - Finding lowest number of a file.

I'm writing a KSH script that will get a file on the command line (such as input.txt), and in this file there is on number per line. The program needs to take the file, read each and determine the lowest number in the file. I currently have a while loop setup that will correctly out put every... (8 Replies)
Discussion started by: denyal
8 Replies

9. Shell Programming and Scripting

Perl ? - How to find and print the lowest and highest numbers punched in by the user?

. . . . . . (3 Replies)
Discussion started by: some124one
3 Replies

10. UNIX for Dummies Questions & Answers

sort with highest wc

Hi :) I'm a unix beginner and i've recently got an assignment to write up a script to print the most common IP address that made requests from a webserver. I'm really lost in this one...and if someone could pls tell me where to start i'll be really greatful ! thanx (1 Reply)
Discussion started by: ymf
1 Replies
Login or Register to Ask a Question