Linux awk Array Operations

Source: Internet
Author: User

The linux awk Array Operation details the use of awk for text processing, which is ultimately its array processing. What are the features of the awk array? What about common operations. Let's take a look at some of the following introductions. Let's take a look at some examples to illustrate their differences. In awk, arrays are called associative arrays, because the bottom mark can be numbers or strings. The array in the awk does not need to be declared in advance or in size. Array elements are initialized with 0 or an empty string, depending on the context. Example: 1. Definition Method 1: You can use a number as an array index (subscript). Tarray [1] = "cheng mo" Tarray [2] = "800927" 2: you can use a string as an array index (subscript) tarray ["first"] = "cheng" Tarray ["last"] = "mo" Tarray ["birth"] = "800927" in use print Tarray [1] will get" and print Tarray [2] and print ["birth"] Both get "800927 ". 2. array-related functions [chengmo @ localhost ~] $ Awk -- versionGNU Awk 3.1.5 version: 3.1 or above. The functions in different versions are not necessarily the same to get the array length (the length method is used) [chengmo @ localhost ~] $ Awk 'in in {info = "it is a test"; lens = split (info, tA, ""); print length (tA), lens ;} '4 4 length' returns the string and array length. split splits the string into an array and returns the length of the array. (Used by asort): [chengmo @ localhost ~] $ Awk 'in in {info = "it is a test"; split (info, tA, ""); print asort (tA);} '4 asort sorts arrays, returns the length of the array. Output array content (unordered, ordered output): [chengmo @ localhost ~] $ Awk 'in in {info = "it is a test"; split (info, tA, ""); for (k in tA) {print k, tA [k];} '4 test1 it2 is3 a... In output, because the array is associated with the array, It is unordered by default. So through... In gets an unordered array. To obtain an ordered array, you must obtain it by subscript. [Chengmo @ localhost ~] $ Awk 'in in {info = "it is a test"; tlen = split (info, tA, ""); for (k = 1; k <= tlen; k ++) {print k, tA [k];} '1 it2 is3 a4 test note: the array subscript starts from 1, different from the c array. To determine whether a key value exists and delete it: an incorrect method: [chengmo @ localhost ~] $ Awk 'in in {tB ["a"] = "a1"; tB ["B"] = "b1"; if (tB ["c"]! = "1") {print "no found" ;}; for (k in tB) {print k, tB [k] ;}} 'nofounda a1b b1c or above has a strange problem. tB ["c"] is not defined, but this key value already exists in the loop. Its value is blank. Please note that, an awk array is an associated array. As long as its key is referenced through an array, the sequence is automatically created and changed. correct judgment method: [chengmo @ localhost ~] $ Awk 'in in {tB ["a"] = "a1"; tB ["B"] = "b1"; if ("c" in tB) {print "OK" ;}; for (k in tB) {print k, tB [k] ;}} 'a a1b b1 if (key in array) this method is used to determine whether the array contains the "key" key value. Delete A key value: [chengmo @ localhost ~] $ Awk 'in in {tB ["a"] = "a1"; tB ["B"] = "b1"; delete tB ["a"]; for (k in tB) {print k, tB [k] ;}} 'B b1 delete array [key] can be deleted, corresponding to the array key, sequence value. 3. Using two-dimensional arrays (using multi-dimensional arrays) An awk multi-dimensional array is essentially a one-dimensional array. To be more precise, awk does not support multi-dimensional arrays in storage. Awk provides a way to logically simulate two-dimensional array access. For example, access like array [2, 4] = 1 is allowed. Awk uses a special string SUBSEP (\ 034) as the split field. In the above example, the key value stored in the associated array is actually 2 \ 0344. For member tests similar to one-dimensional arrays, multi-dimensional arrays can use the if (I, j) in array) syntax, but the subscript must be placed in parentheses. Similar to circular access to one-dimensional arrays, multi-dimensional arrays use syntax such as for (item in array) to traverse arrays. Different from a one-dimensional array, a multi-dimensional array must use the split () function to access a separate subscript component. Split (item, subscr, SUBSEP) [chengmo @ localhost ~] $ Awk 'in in {for (I = 1; I <= 9; I ++) {for (j = 1; j <= 9; j ++) {tarr [I, j] = I * j; print I, "*", j, "=", tarr [I, j] ;}} '1*1 = 11*2 = 21*3 = 31*4 = 41*5 = 51*6 = 6 ...... Array content can be obtained through array [k, k2] reference. Method 2: [chengmo @ localhost ~] $ Awk 'in in {for (I = 1; I <= 9; I ++) {for (j = 1; j <= 9; j ++) {tarr [I, j] = I * j ;}for (m in tarr) {split (m, tarr2, SUBSEP); print tarr2 [1], "*", tarr2 [2], "=", tarr [m];}'

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.