Linuxawk array sorting methods

Source: Internet
Author: User
The awk array is an associated array. For... During in loop output, the unordered array is printed by default.


Copy codeThe code is as follows:
[Chengmo @ localhost ~] $ Awk 'In in {info = "this is a test"; split (info, tA, ""); for (k in tA) {print k, tA [k] ;}}'
4 test
1 this
2 is
3

If you want to output data in sequence, use the key-value positioning method.


Copy codeThe code is as follows:
[Chengmo @ localhost ~] $ Awk 'In in {info = "this is a test"; slen = split (info, tA, ""); for (I = 1; I <= slen; I ++) {print I, tA [I] ;}}'
1 this
2 is
3
4 test

I. It is supported only through the built-in functions (used by asort and asorti) awk 3.1 or later.

1. asort instructions
Srcarrlen = asort [srcarr, dscarr] the default return value is: the length of the original array. if the input parameter is dscarr, the sorted array is assigned to dscarr.


Copy codeThe code is as follows:
[Chengmo @ localhost ~] $ Awk 'In in {
A [100] = 100;
A [2] = 224;
A [3] = 34;
Slen = asort (a, tA );
For (I = 1; I <= slen; I ++)
{Print I, tA [I];}
}'
1 34
2 100
3 224

Asort only sorts values, so the original first key value is lost.

2. asorti instructions


Copy codeThe code is as follows:
[Chengmo @ localhost ~] $ Awk 'In in {
A ["d"] = 100;
A ["a"] = 224;
A ["c"] = 34;
Slen = asorti (a, tA );
For (I = 1; I <= slen; I ++)
{Print I, tA [I], a [tA [I];}
}'
1 a 224
2 c 34
3 d 100

Asorti sorts key values (string type) and puts the generated array into the tA.

II. sort data sent to sort through pipelines


Copy codeThe code is as follows:
[Chengmo @ localhost ~] $ Awk 'In in {
A [100] = 100;
A [2] = 224;
A [3] = 34;
For (I in)
{Print I, a [I] | "sort-r-n-k2 ";}
}'
2 224
100 100
3 34

Sort by MPs queue and send it to the external program "sort".-r is sorted from big to small,-n is sorted by number, and-k2 is sorted by 2nd columns. By throwing data to the sort command of the 3rd party, all problems become very simple. If the key value is sorted,-k2 is changed to-k1.


Copy codeThe code is as follows:
[Chengmo @ localhost ~] $ Awk 'In in {
A [100] = 100;
A [2] = 224;
A [3] = 34;
For (I in)
{Print I, a [I] | "sort-r-n-k1 ";}
}'
100 100
3 34
2 224

III. Custom sorting functions

Awk custom function structure:


Copy codeThe code is as follows:
Function funname (p1, p2, p3)
{
Staction;
Return value;
}

The above is the expression of the awk user-defined function. by default, all input parameters are passed in as references. The return value can only be numeric or numeric. The array type cannot be returned. If the array type is returned. The parameter must be input. .

Array type returned by awk

Awk 'function test (ary) {for (I = 0; I <10; I ++) {ary [I] = I;} return I ;} BEGIN {n = test (array); for (I = 0; I

Sorting function


Copy codeThe code is as follows:
# Input a one-dimensional array by arr

# Key sorting Type 1: Sort by value 2: Sort by key value

# Datatype Comparison Type 1 sort by number 2 Sort by string

# Array returned by tarr sorting

# Splitseq: split string between the key and the value in the string array

# Return array length

# Implementation idea: sort the original array a ['A'] = 100 to a [1] = a separator 100, and display the content recursively according to the subscript. This sorting is performed in the bubble mode.

Function sortArr (arr, key, datatype, tarr, splitseq)
{

If (key ~ /[^ 1-2]/)
{Return tarr ;}
For (k in arr)
{
Tarr [++ alen] = (k "" splitseq "" arr [k]);
}

For (m = 1; m <= alen; m ++)
{
For (n = 1; n <= alen-m-1; n + +)
{
Split (tarr [m], tm, splitseq );
Split (tarr [n + 1], tn, splitseq );

Tnum = tarr [m];
If (datatype = 1)
{
If (tm [key] + 0
{
Tarr [m] = tarr [n + 1];
Tarr [n + 1] = tnum;
}
}
Else
{
If (tm [key] "") <(tn [key] "")
{
Tarr [m] = tarr [n + 1];
Tarr [n + 1] = tnum;
}
}
}
}
Return alen;
}

The complete code is as follows:


Copy codeThe code is as follows:
[Chengmo @ centos5 ~] $ Awk 'In in {
A ["a"] = 100;
A ["B"] = 110;
A ["c"] = 10;
Splitseq = "% ";
Alen = sortArr (a, 2, 1, tarr, splitseq );
For (m = 1; m <= alen; m ++)
{
Split (tarr [m], ta, splitseq );
Print m, ta [1], ta [2];
}
}
Function sortArr (arr, key, datatype, tarr, splitseq)
{

If (key ~ /[^ 1-2]/)
{Return tarr ;}
For (k in arr)
{
Tarr [++ alen] = (k "" splitseq "" arr [k]);
}

For (m = 1; m <= alen; m ++)
{
For (n = 1; n <= alen-m-1; n + +)
{
Split (tarr [m], tm, splitseq );
Split (tarr [n + 1], tn, splitseq );

Tnum = tarr [m];
If (datatype = 1)
{
If (tm [key] + 0
{
Tarr [m] = tarr [n + 1];
Tarr [n + 1] = tnum;
}
}
Else
{
If (tm [key] "") <(tn [key] "")
{
Tarr [m] = tarr [n + 1];
Tarr [n + 1] = tnum;
}
}
}
}
Return alen;
}
'

1 B 110
2 a 100
3 c 10

The above are some methods for sorting awk arrays. For sorting a small amount of data, the performance of using a user-defined function is high, and you do not need to enable the process again. For a large amount of data, sorting 2nd methods is still very good.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.