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

前端 未结 8 1130
时光取名叫无心
时光取名叫无心 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

提交回复
热议问题