Sponsored Content
Top Forums UNIX for Dummies Questions & Answers gawk asort to sort record groups based on one subfield Post 302711717 by lucasvs on Sunday 7th of October 2012 11:33:34 PM
Old 10-08-2012
Quote:
ou 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.
So you mean I should
1st) sort by field1 and generate a first output
2nd) use this output to sort by subfield 2 and generate the final output.

I tried things like below but still doesn't work.
Code:
BEGIN{FS=OFS="/"}

{
# sort by field1
    array[$1] = $0

    asort(array)

# first output
    for (i in array)
        $0 = array[i]

# redefine fields in first output    
        split($0, rec, FS)
        rec[$2] = $0

        split($0, sub, " ")
        aut[++a] = sub[2]

# sort by subfield2 
        n = asort(aut)

# print final output
        for (j=1; j<=n; j++)
            print array[aut[j]]
    
}

 

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
SORT(3) 								 1								   SORT(3)

sort - Sort an array

SYNOPSIS
bool sort (array &$array, [int $sort_flags = SORT_REGULAR]) DESCRIPTION
This function sorts an array. Elements will be arranged from lowest to highest when this function has completed. PARAMETERS
o $array - The input array. o $sort_flags - The optional second parameter $sort_flags may be used to modify the sorting behavior using these values: Sorting type flags: o SORT_REGULAR - compare items normally (don't change types) o SORT_NUMERIC - compare items numerically o SORT_STRING - compare items as strings o SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale(3) o SORT_NATURAL - compare items as strings using "natural ordering" like natsort(3) o SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively RETURN VALUES
Returns TRUE on success or FALSE on failure. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.4.0 | | | | | | | Added support for SORT_NATURAL and | | | SORT_FLAG_CASE as $sort_flags | | | | | 5.0.2 | | | | | | | Added SORT_LOCALE_STRING | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 sort(3) example <?php $fruits = array("lemon", "orange", "banana", "apple"); sort($fruits); foreach ($fruits as $key => $val) { echo "fruits[" . $key . "] = " . $val . " "; } ?> The above example will output: fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange The fruits have been sorted in alphabetical order. Example #2 sort(3) example using case-insensitive natural ordering <?php $fruits = array( "Orange1", "orange2", "Orange3", "orange20" ); sort($fruits, SORT_NATURAL | SORT_FLAG_CASE); foreach ($fruits as $key => $val) { echo "fruits[" . $key . "] = " . $val . " "; } ?> The above example will output: fruits[0] = Orange1 fruits[1] = orange2 fruits[2] = Orange3 fruits[3] = orange20 The fruits have been sorted like natcasesort(3). NOTES
Note This function assigns new keys to the elements in $array. It will remove any existing keys that may have been assigned, rather than just reordering the keys. Note Like most PHP sorting functions, sort(3) uses an implementation of Quicksort. The pivot is chosen in the middle of the partition resulting in an optimal time for already sorted arrays. This is however an implementation detail you shouldn't rely on. Warning Be careful when sorting arrays with mixed types values because sort(3) can produce unpredictable results. SEE ALSO
asort(3), The comparison of array sorting functions. PHP Documentation Group SORT(3)
All times are GMT -4. The time now is 01:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy