我怎样才能得到一个AWK数组的长度? [英] How can I get the length of an array in awk?
本文介绍了我怎样才能得到一个AWK数组的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此命令
echo "hello world" | awk '{split($0, array, " ")} END{print length(array) }'
不为我工作,给此错误消息
does not work for me and gives this error message
AWK:线路1:非法引用数组的数组
awk: line 1: illegal reference to array array
为什么?
推荐答案
当您分割一个数组,返回元素的#,所以可以说
when you split an array, the # of elements is returned, so you can say
echo "hello world" | awk '{n=split($0, array, " ")} END{print n }'
# ------------------------^^^--------------------------------^^
输出为
2
我希望这有助于。
I hope this helps.
这篇关于我怎样才能得到一个AWK数组的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文