Multiple methods of awk array sorting

Source: Internet
Author: User
Awk array sorting is an associated array because of the awk array. For & hellip; in loop output, unordered arrays are printed by default. [Chengmo @ localhost ~] $ Awk & amp; #39; BEGIN {info & quot; thisisatest & quot; split (info, tA, & quot ;); for (multiple methods to sort the ki awk array because of the awk array, it is an associated 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  

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.