Sponsored Content
Top Forums UNIX for Dummies Questions & Answers gawk asort to sort record groups based on one subfield Post 302711711 by Don Cragun on Sunday 7th of October 2012 10:56:26 PM
Old 10-07-2012
The versions of awk that I use (on OS X) don't have the asort() and asorti() functions, but I have read the gawk man page. Unlike the sort utility, there is no way to specify a sort key for these functions; they always sort the array using the entire contents of the string as the sort key. If you want to use asort() in gawk to sort with field 1 as your primary sort key and the second part of field 2 as your secondary key; you need to prepend each line in your array with primary and secondary sort fields, use asort() or asorti() to sort the modified records, and then strip off the added sort fields when you print (or otherwise process) the results.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing \n within a record (awk/gawk)

I am using a solution that was provided by a member: awk '{s=$0;if(length(s) < 700){getline; s=s " " $0}printf("%s\n",s)}' This scans through a file and removes '\n' within a record but not the record delimiter. However, there are instances where there are MULTIPLE instances of '\n'... (10 Replies)
Discussion started by: CKT_newbie88
10 Replies

2. Shell Programming and Scripting

Gawk / Awk Merge Lines based on Key

Hi Guys, After windows died on my netbook I installed Lubuntu and discovered Gawk about a month ago. After using Excel for 10+ years I'm amazed how quick and easily Gawk can process data but I'm stuck with a little problem merging data from multiple lines. I'm an SEO Consultant and provide... (9 Replies)
Discussion started by: Jamesfirst
9 Replies

3. Programming

Help with sort and keep data record to calculate N50 in c

Input_file_1 #content_1 A #content_2 AF #content_3 AAR #content_4 ASEI #content_5 AS #content_6 ADFSFGS Rules: 1. Based on c program to calculate content of each "#". Result getting from the above Input_file_1 are 1,2,3,4,2,7; 2. Sort length on reverse order (descending order).... (5 Replies)
Discussion started by: cpp_beginner
5 Replies

4. Shell Programming and Scripting

sort file specifying record length

I've been searching high and low for this...but, maybe I'm just missing something. I have a file to be sorted that, unfortunately, contains binary data at the end of the line. As you may guess, this binary data may contain a newline character, which messes up the sort. I think I could resolve this... (5 Replies)
Discussion started by: jcagle
5 Replies

5. Shell Programming and Scripting

AWK exclude first and last record, sort and print

Hi everyone, I've really searched for a solution to this and this is what I found so far: I need to sort a command output (here represented as a "cat file" command) and from the second down to the second-last line based on the second row and then print ALL the output with the specified section... (7 Replies)
Discussion started by: dentex
7 Replies

6. Shell Programming and Scripting

Don't understand AWK asort behaviour

Hello, I have the following script : BEGIN { print "1 ***"; split("abc",T,""); T="e"; T="z"; T="y"; for (i in T) printf("%i:%s ",i,T); print ""; for (i=1; i<=length(T); i++) printf(T); print "" print "2 ***"; asort(T,U); for (i in U) printf("%i:%s ",i,U); ... (3 Replies)
Discussion started by: jgilot
3 Replies

7. Shell Programming and Scripting

Match groups of capital words using gawk

Hi I'd like to extract from a text file, using gawk, the groups of words beginning with a capital letter, that are not at the begining of a sentence (i.e. Not after a full stop and a pace ". "), including special characters like registered or trademark (® or ™ ). For example I would like to... (1 Reply)
Discussion started by: louisJ
1 Replies

8. Shell Programming and Scripting

Sort html based on .jar, .war file names and still keep text within three groups.

Output from zipdiff GNU EAR comparison tool produces output in html divided into three sections "Added, Removed, Changed". I want the output to be sorted by jar or war file. <html> <body> <table> <tr> <td class="diffs" colspan="2">Added </td> </tr> <tr><td> <ul>... (5 Replies)
Discussion started by: kchinnam
5 Replies

9. UNIX for Beginners Questions & Answers

Gawk: PROCINFO["sorted_in"] does not sort my numerical array values

Hi, PROCINFO seems to be a great function but I don't manage to make it works. input: B,A,C B B,B As an example, just want to count the occurence of each letter across the input and sort them by decreased order. Wanted output: B 4 A 1 C 1 When I use this command, the PROCINFO... (4 Replies)
Discussion started by: beca123456
4 Replies

10. Shell Programming and Scripting

Best way to sort file with groups of text of 4-5 lines by the first one

Hi, I have some data I have taken from the internet in the following scheme: name direction webpage phone number open hours menu url book url name ... Of course the only line that is mandatory is the name wich is the one I want to sort by. I have the following sed & awk script that... (3 Replies)
Discussion started by: devmsv
3 Replies
asort(1)						      General Commands Manual							  asort(1)

NAME
asort - Sorts or merges files and supports multiple collating weight sequences SYNOPSIS
asort [-m] [-o output_file] [-Abdfinruv] [-Ccollate_sequence] [-k keydef]... [-t character] [-T directory] [-y] [kilobytes] [-z record_size]... file... asort -c [-u] [-Abdfinruv] [-Ccollate_sequence] [-k keydef]... [-t character] [-T directory] [-y] [kilobytes] [-z record_size]... file... The following syntax is maintained for backward compatibility but may be withdrawn in a future release: asort [-Abcdfimnruv] [-Ccol- late_sequence] [-o output_file] [-t character] [-T directory] [-y] [kilobytes] [-z record_size] [+fskip] [.cskip] [-fskip] [.cskip] [-bdfinr]... file... OPTIONS
The asort command includes the same options as the sort command (see sort(1)) in addition to the following options: Specifies the collating weight sequence to be used in sorting the data files. When this option is specified, the asort command does not use the collating table from the locale database. Instead, the command uses a set of special system and user collating tables to determine the collating weights of characters, including user-defined characters (UDCs). The collate_sequence argument can be in long form (for example, "Pinyin Radical Stroke") or short form (for example, prs). The code- set of the locale determines which collation weight names can be specified for collate_sequence. The following list specifies the long and short collation weight names that are valid for supported codesets. For DEC Hanzi: Pinyon (or p) Radical (or r) Stroke (or s) For DEC Hanyu, Taiwanese EUC, and BIG-5: Phonetic (or p) Radical (or r) Stroke (or s) Uses a breadth-first sorting mechanism instead of the default depth-first mechanism to sort the input data. To have any effect, the -v option must be used together with the -C option. DESCRIPTION
The asort command sorts lines in its input files and writes the result to standard output. The asort command is similar to the sort com- mand. See the sort(1) reference page for information about features the two commands have in common. The asort command provides additional features for processing multiple collating weight sequences used with Asian languages, such as Chi- nese. For example, pinyon (p), stroke (s), and radical (r) are three dimensions along which characters can be ordered in Simplified Chi- nese. The -C option allows users to specify the priority level that these dimensions have during sorting. For example, -C srp specifies that characters should be sorted first by stroke, then by radical, then by pinyon. The specified sequence is applied to user-defined char- acters (UDCs) as well as to standard characters. When the -C option is specified, the default behavior of the asort command is to use a depth-first sorting mechanism to sort the input files. With the depth-first mechanism, pairs of multibyte characters in a sort field are compared by exhausting all the specified collat- ing weights and/or internal codes one at a time until the collating order is resolved. Only when two characters are identical is the next pair of characters compared. The depth-first sorting mechanism is also called character sorting. However, the asort command provides the -v option to use the Asian VMS-like breadth-first sorting mechanism. With the breadth-first mecha- nism, pairs of multibyte characters in a sort field are compared using the first collating weight for all the characters in the sort field first. Only when two sets of data in a sort field are computed to have the same collating order are succeeding collating weights used for resolving the collating order. The breadth-first sorting mechanism is sometimes called string sorting. NOTES
Currently, the asort command is supported for use only with Chinese codesets. EXIT STATUS
The asort command returns the following exit values: All input files were output successfully, or -c was specified and the input file was correctly sorted. If -c was specified, the file was not ordered as specified, or if the -c and -u options were both specified, two input lines were found with equal keys. An error occurred. EXAMPLES
Unless stated otherwise, the following examples assume the locale setting is zh_TW.dechanyu: To perform character sorting first by stroke and then by radical, enter: asort -C"Stroke Radical" names This command displays the lines in names sorted in ascending order according to the number of strokes in characters. If the number of strokes happen to be the same for two characters, the radicals of the characters determine how the characters are ordered.An alternative short form of the same command is as follows: asort -Csr names To perform string sorting first by stroke and then by radical in a way similar to the sort command available on an Asian VMS system, enter: asort -v -C"Stroke Radical" names SEE ALSO
Commands: sort(1) Functions: setlocale(3) Files: locale(4) Others: Chinese(5), i18n_intro(5) asort(1)
All times are GMT -4. The time now is 02:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy