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

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

    When you split an array, the number of elements is returned, so you can say:

    echo "hello world" | awk '{n=split($0, array, " ")} END{print n }'
    # ------------------------^^^--------------------------------^^
    

    Output is:

    2
    

提交回复
热议问题