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

后端 未结 9 939
不思量自难忘°
不思量自难忘° 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条回答
  •  猫巷女王i
    2020-11-29 17:13

    Please be more specific! What do you mean by "it doesn't work"? Post the exact output (or error message), your OS and awk version:

    % awk -F\| '{
      for (i = 0; ++i <= NF;)
        print i, $i
      }' <<<'12|23|11'
    1 12
    2 23
    3 11
    

    Or, using split:

    % awk '{
      n = split($0, t, "|")
      for (i = 0; ++i <= n;)
        print i, t[i]
      }' <<<'12|23|11'
    1 12
    2 23
    3 11
    

    Edit: on Solaris you'll need to use the POSIX awk (/usr/xpg4/bin/awk) in order to process 4000 fields correctly.

提交回复
热议问题