Multiple methods of awk array sorting

Source: Internet
Author: User

Awk array sorting is an associated array because of the awk array. For... In loop output, the unordered array is printed by default. [Chengmo @ localhost ~] $ Awk 'in in {info = "this is a test"; split (info, tA, ""); for (k in tA) {print k, tA [k] ;}}'4 test1 this2 is3 a. If you want to output data in sequence, use the key value Positioning method. [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 this2 is3 a4 test 1, used by the built-in function (asort, asorti) awk 3.1 and later versions support asort usage instructions srcarrlen = asort [srcarr, dscarr] The default return value is: the length of the original array. If the input parameter dscarr is used, the sorted array is assigned to dscarr. [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 342 1003 224 asort only sorts the values, therefore, the original key value is lost. 2. asorti instructions [chengmo @ localhost ~] $ Awk 'in in {a ["d"] = 100; a ["a"] = 224; a ["c"] = 34; slen = asorti (, tA); for (I = 1; I <= slen; I ++) {print I, tA [I], a [tA [I];} '1 a 2242 c 343 d 100 asorti sorts the key values (string type) and puts the new array into the tA. 2. sort by MPs queue to sort [chengmo @ localhost ~] $ Awk 'in in {a [100] = 100; a [2] = 224; a [3] = 34; for (I in a) {print I, a [I] | "sort-r-n-k2";} '2 224100 1003 34. sort by MPs queue and send it to the external program "sort".-r is from large 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. [Chengmo @ localhost ~] $ Awk 'in in {a [100] = 100; a [2] = 224; a [3] = 34; for (I in a) {print I, a [I] | "sort-r-n-k1";} '2017 100 1003 342 3. Custom sorting function awk custom function structure: function funname (p1, p2, p3) {staction; return value;} and above are: awk User-Defined Function Representation. By default, all input parameters are passed in as references. return values can only be numeric or numeric. The array type cannot be returned. If the array type is returned. The parameter must be input.. Awk returns the array type awk 'function test (ary) {for (I = 0; I <10; I ++) {ary [I] = I;} return I ;} BEGIN {n = test (array); for (I = 0; I <n; I ++) {print array [I] ;}} 'sorting function # input a one-dimensional array by arr # key sorting type 1 is sort by value 2 by key value # datatype comparison type 1 by number 2 by string # array returned by tarr sort # splitseq split the string between the middle key and the value # return array length # implementation idea, sort the original array a ['a'] = 100 and change it to a [1] = a separator 100. Then, recursively display the content 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 <tn [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: [chengmo @ cen Tos5 ~] $ 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 <tn [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 1102 a 1003 c 1 More than 0 is the awk array sorting method. 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.