How to split a delimited string into an array in awk?

后端 未结 9 906
不思量自难忘°
不思量自难忘° 2020-11-29 16:58

How to split the string when it contains pipe symbols | in it. I want to split them to be in array.

I tried

echo \"12:23:11\" | awk \'{s         


        
相关标签:
9条回答
  • 2020-11-29 17:30

    To split a string to an array in awk we use the function split():

     awk '{split($0, a, ":")}'
     #           ^^  ^  ^^^
     #            |  |   |
     #       string  |   delimiter
     #               |
     #               array to store the pieces
    

    If no separator is given, it uses the FS, which defaults to the space:

    $ awk '{split($0, a); print a[2]}' <<< "a:b c:d e"
    c:d
    

    We can give a separator, for example ::

    $ awk '{split($0, a, ":"); print a[2]}' <<< "a:b c:d e"
    b c
    

    Which is equivalent to setting it through the FS:

    $ awk -F: '{split($0, a); print a[1]}' <<< "a:b c:d e"
    b c
    

    In gawk you can also provide the separator as a regexp:

    $ awk '{split($0, a, ":*"); print a[2]}' <<< "a:::b c::d e" #note multiple :
    b c
    

    And even see what the delimiter was on every step by using its fourth parameter:

    $ awk '{split($0, a, ":*", sep); print a[2]; print sep[1]}' <<< "a:::b c::d e"
    b c
    :::
    

    Let's quote the man page of GNU awk:

    split(string, array [, fieldsep [, seps ] ])

    Divide string into pieces separated by fieldsep and store the pieces in array and the separator strings in the seps array. The first piece is stored in array[1], the second piece in array[2], and so forth. The string value of the third argument, fieldsep, is a regexp describing where to split string (much as FS can be a regexp describing where to split input records). If fieldsep is omitted, the value of FS is used. split() returns the number of elements created. seps is a gawk extension, with seps[i] being the separator string between array[i] and array[i+1]. If fieldsep is a single space, then any leading whitespace goes into seps[0] and any trailing whitespace goes into seps[n], where n is the return value of split() (i.e., the number of elements in array).

    0 讨论(0)
  • 2020-11-29 17:31

    Joke? :)

    How about echo "12|23|11" | awk '{split($0,a,"|"); print a[3] a[2] a[1]}'

    This is my output:

    p2> echo "12|23|11" | awk '{split($0,a,"|"); print a[3] a[2] a[1]}'
    112312
    

    so I guess it's working after all..

    0 讨论(0)
  • 2020-11-29 17:34

    Actually awk has a feature called 'Input Field Separator Variable' link. This is how to use it. It's not really an array, but it uses the internal $ variables. For splitting a simple string it is easier.

    echo "12|23|11" | awk 'BEGIN {FS="|";} { print $1, $2, $3 }'
    
    0 讨论(0)
提交回复
热议问题