How can I get the length of an array in awk?

前端 未结 8 1108
时光取名叫无心
时光取名叫无心 2020-12-03 04:22

This command

echo \"hello world\" | awk \'{split($0, array, \" \")} END{print length(array) }\'

does not work for me and gives this error m

相关标签:
8条回答
  • 2020-12-03 04:58

    Try this if you are not using gawk.

    awk 'BEGIN{test="aaa bbb ccc";a=split(test, ff, " "); print ff[1]; print a; print ff[a]}'
    

    Output:

    aaa
    3
    ccc
    

    8.4.4 Using split() to Create Arrays http://docstore.mik.ua/orelly/unix/sedawk/ch08_04.htm

    0 讨论(0)
  • 2020-12-03 04:58
    echo "hello world" | awk '{lng=split($0, array, " ")} END{print lng) }'
    
    0 讨论(0)
提交回复
热议问题