Linux awk array operation details

  
 

Text processing with awk, and ultimately its array processing. So what are the characteristics of awk arrays, what about common operations? Let's take a look at some of the following introductions. We will explain the differences between them in the example. Arrays in awk are called associative arrays because the underlying tags can be either numbers or strings. Arrays in awk do not have to be declared in advance, nor do they have to be declared. Array elements are initialized with 0 or an empty string, depending on the context. For example:


First, define the method


1: You can use the value as an array index (subscript)

Tarray[ ,null,null,3],1]=“cheng mo”Tarray[2]=“800927”

2: can use string as array index (subscript)

Tarray[“first”]=“cheng ”Tarray[“last”]=”mo”Tarray[“birth”]=”800927”

In use of print Tarray[1] will get ”cheng mo” and print Tarray[2] and Print[“birth”] will get ”800927”


Second, array related functions

[chengmo@localhost ~]$ awk --versionGNU Awk 3.1.5

The usage version is: 3.1 or higher The following functions are not necessarily the same in different versions

  • Get the length of the array (used by the length method)

    [chengmo@localhost ~]$ awk 'BEGIN{info="it Is a test";lens=split(info,tA," ");print length(tA),lens;}'4 4

    length returns the length of the string and the array, split splits the string For an array, it will also return the split to get the length of the array.


    (asort):

    [chengmo@localhost ~]$ awk 'BEGIN{info="it is a test";split(info,tA, " ");print asort(tA);}'4

    asort sorts the array and returns the length of the array.



  • Output array contents (unordered, ordered output):

    [chengmo@localhost ~]$ awk 'BEGIN{info=" ;it is a test";split(info,tA," ");for(k in tA){print k,tA[k];}}'4 test1 it2 is3 a


    for…in Output, because arrays are associative arrays, the default is unordered. So get an unordered array with for…in. If you need to get an ordered array, you need to get it by subscript.


    [chengmo@localhost ~]$ awk 'BEGIN{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 at 1 and is not the same as the c array.




  • Determining the existence of key values ​​and deleting key values:

    An error judgment method:

    [chengmo@localhost ~]$ awk 'BEGIN{tB["a"]="a1";tB["b"]="b1";if(tB["c"]!=" ;1"){print "no found";};for(k in tB){print k,tB[k];}}' no founda a1b b1c


    Strange problem, tB[“c”] is not defined, but when looping, it is found that the key value already exists, its value is empty, here we need to note that the awk array is an associative array, as long as its key is referenced by the array, Automatically create a modified sequence.


    Correct judgment method:

    [chengmo@localhost ~]$ awk 'BEGIN{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 value.


    Delete key:

    [chengmo@localhost ~]$ awk 'BEGIN{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, the sequence value.


    Three, two-dimensional array use (multidimensional array use)

    Awk's multidimensional array is essentially a one-dimensional array, more precisely, awk is stored on the Multidimensional arrays are not supported. Awk provides a way to logically simulate the access of a two-dimensional array. For example, access such as array[2,4] = 1 is allowed. Awk uses a special string SUBSEP (\\034) as the split field. In the above example, the associated array array stores the actual value of 2\\0344.


    Similar to the one-dimensional array of member tests, multidimensional arrays can use the syntax of if ( (i,j) in array), but the subscript must be placed in parentheses. Similar to circular access for one-dimensional arrays, multidimensional arrays traverse arrays using a syntax like for ( item in array ). Unlike one-dimensional arrays, multidimensional arrays must use the split() function to access individual subscript components. Split ( item, subscr, SUBSEP)


    [chengmo@localhost ~]$ awk 'BEGIN{

    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

    ……

    can be passed to array[k , k2] reference to get the contents of the array.


    Method 2:

    [chengmo@localhost ~]$ awk 'BEGIN{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[ ,null,null,3],1],"*",tarr2[2],"=",tarr[m];}}'


    The above is related to the processing of arrays by awk, hope Useful to everyone.

  • Copyright © Windows knowledge All Rights Reserved