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

后端 未结 9 942
不思量自难忘°
不思量自难忘° 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:29

    I know this is kind of old question, but I thought maybe someone like my trick. Especially since this solution not limited to a specific number of items.

    # Convert to an array
    _ITEMS=($(echo "12|23|11" | tr '|' '\n'))
    
    # Output array items
    for _ITEM in "${_ITEMS[@]}"; do
      echo "Item: ${_ITEM}"
    done
    

    The output will be:

    Item: 12
    Item: 23
    Item: 11
    

提交回复
热议问题