Sponsored Content
Full Discussion: Sort and vectors on awk
Top Forums UNIX for Dummies Questions & Answers Sort and vectors on awk Post 302771639 by Don Cragun on Thursday 21st of February 2013 06:36:16 AM
Old 02-21-2013
Here are three ways to sort your array that should work even if you don't have gawk:
Code:
awk '{  # Read the input file in to array a.
        a[$1] = $2
}       
END {   # Use the sort utility to print array a in sorted order.
        print "print | sort:"
        cmd = "sort -k2nr" # Sort 2nd field as a numeric value in reverse order
        for(i in a) print i, a[i] | cmd
        close(cmd)

        # Copy a into b and c using an insertion sort to keep b and c sorted as
        # entries are added.
        for(k in a) {
                b[++nc] = k
                c[nc] = a[k]
                for(i = nc; i > 1; i--)
                        if(c[i] > c[i - 1]) {
                                tmp = b[i - 1]
                                b[i - 1] = b[i]
                                b[i] = tmp
                                tmp = c[i - 1]
                                c[i - 1] = c[i]
                                c[i] = tmp
                        } else  break;
        }
        print "print sorted b and c"
        for(i = 1; i <= nc; i++) print b[i], c[i]

        # Loop through original array printing and deleting the largest value.
        # This loop destroys the array as it prints it.
        print "print largest value in loop:"
        for(i = 1; i <= NR; i++) {
                max = 0
                for(j in a) if(a[j] > max) {
                        max = a[j]
                        mj = j
                }
                print mj, a[mj]
                delete a[mj]
        }
}' file.txt

On Solaris/SunOS systems, use /usr/xpg4/bin/awk or nawk instead of awk. On AIX or any other UNIX or Linux system, this should be fine as shown.

With your sample input, this awk script produces:
Code:
print | sort:
Caps 12
cols 10
cans 9
print sorted b and c
Caps 12
cols 10
cans 9
print largest value in loop:
Caps 12
cols 10
cans 9

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help, awk sed sort

Hi,everyone: I'm new to shell, and now get trouble with some script: line=`/usr/xpg4/bin/awk '/^(*\|){2}'"$CR"'/ {print $0}' ${TIER4FILE}|grep -v OSNAME=`sed -n ''$LINE'p' $DATALOC/os` sort +1 /tmp/LhasaCRs2 > /tmp/LhasaCRs1 I cannot understand the "{2}" here. My mentor said it... (1 Reply)
Discussion started by: mycoy
1 Replies

2. Homework & Coursework Questions

awk sort help

1. The problem statement, all variables and given/known data: I dont know what I do wrong, I am trying to create shell programming database: I have this command first: && > $fname ... echo $Name:$Surname:$Agency:$Tel:$Ref: >> $fname then I have echo " Name Surname Agency Tel... (2 Replies)
Discussion started by: jeht
2 Replies

3. Shell Programming and Scripting

Sort in AWK

Hi, I usually use Access to sort data however for some reason its not working. Our systems guys and myself cannot figure it out so ive tried to use AWK to do the sorting. The file is made up of single lines in the format ... (4 Replies)
Discussion started by: eknryan
4 Replies

4. Shell Programming and Scripting

Sort and count using AWK

Hi, I've a fixed width file where I need to count the number of patterns from each line between characters 1 to 15 . so can we sort them and get a count for each pattern on the file between 1 to 15 characters. 65795648617522383763831552 410828003265795648 6175223837... (5 Replies)
Discussion started by: rudoraj
5 Replies

5. Shell Programming and Scripting

Using Awk to efficiently substitute values using 3 vectors

I'm trying to efficiently combine the fields of two vectors (vectors b and c) into a new vector (vector d) as defined by instructions from a 3rd vector (vector a). So vector a has either a 1 or 2 in each field specifying which vector (b or c respectively) should go into that field. Vector a is... (4 Replies)
Discussion started by: LaTortuga
4 Replies

6. Shell Programming and Scripting

Sort with Awk, sed ....

I want to print out the lines that have the max value in column 3. and count the occurrence of column 1; if there are more than one occurrences, line with highest column 2 value will be printed. I have this data: input: AV 234 25 AV 256 76 AS 421 34 AV 124 76 BD 136 71 BD 241 76 AW... (10 Replies)
Discussion started by: aydj
10 Replies

7. Shell Programming and Scripting

awk sort

input file abc1 abc23 abc12 abc15 output abc1 abc12 abc15 abc23 (9 Replies)
Discussion started by: yanglei_fage
9 Replies

8. Shell Programming and Scripting

how to sort inside awk

Hi guys I have a problem trying to sort output produced with the help of 'Awk'. After "grepping" the pattern out of the file and sorting it, sort command acts a little strange: $ grep -w -n -i "p*cmo" /bb/data/rmt4db.lrl | sort -r 32:P1096CMO 63836 344 passthru 31:P1084CMO 121335 329 passthru... (3 Replies)
Discussion started by: aoussenko
3 Replies

9. Shell Programming and Scripting

Sort String using awk

Hi, I need help to sort string using awk. I don't want to use sed or perl as I want to add this functionality in my existing awk script Basically I have a variable in AWK which is string with comma separated value. I want to sort that string before using that variable in further processing for... (10 Replies)
Discussion started by: rocky.community
10 Replies
Cz::Sort(3pm)						User Contributed Perl Documentation					     Cz::Sort(3pm)

NAME
Cz::Sort - Czech sort SYNOPSIS
use Cz::Sort; my $result = czcmp("_x j&a", "_&p"); my @sorted = czsort qw(plachta plaoka Planieka planieka plani); print "@sorted "; DESCRIPTION
Implements czech sorting conventions, indepentent on current locales in effect, which are often bad. Does the four-pass sort. The idea and the base of the conversion table comes from Petr Olsak's program csr and the code is as compliant with CSN 97 6030 as possible. The basic function provided by this module, is czcmp. If compares two scalars and returns the (-1, 0, 1) result. The function can be called directly, like my $result = czcmp("_x j&a", "_&p"); But for convenience and also because of compatibility with older versions, there is a function czsort. It works on list of strings and returns that list, hmm, sorted. The function is defined simply like sub czsort { sort { czcmp($a, $b); } @_; } standard use of user's function in sort. Hashes would be simply sorted @sorted = sort { czcmp($hash{$a}, $hash{$b}) } keys %hash; Both czcmp and czsort are exported into caller's namespace by default, as well as cscmp and cssort that are just aliases. This module comes with encoding table prepared for ISO-8859-2 (Latin-2) encoding. If your data come in different one, you might want to check the module Cstocs which can be used for reencoding of the list's data prior to calling czsort, or reencode this module to fit your needs. VERSION
0.68 SEE ALSO
perl(1), Cz::Cstocs(3). AUTHOR
(c) 1997--2000 Jan Pazdziora <adelton@fi.muni.cz>, http://www.fi.muni.cz/~adelton/ at Faculty of Informatics, Masaryk University, Brno perl v5.10.1 2000-05-16 Cz::Sort(3pm)
All times are GMT -4. The time now is 04:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy