华域联盟 linux shell Shell中统计字符串中单词的个数的几种方法

Shell中统计字符串中单词的个数的几种方法

Shell中求字符串中单词的个数的几种方法

方法一:

[linux@host ~]# echo 'one two three four five' | wc -w
5

方法二:

[linux@host ~]# echo 'one two three four five' | awk '{print NF}'
5

方法三:

[linux@host ~]# s='one two three four five' 
[linux@host ~]# set ${s}
[linux@host ~]# echo $#
5

方法四:

[linux@host ~]# s='one two three four five' 
[linux@host ~]# a=($s)
[linux@host ~]# echo ${#a[@]}
5

方法五:

[linux@host ~]# s='one two three four five' 
[linux@host ~]# echo $s | tr ' ' '\n' | wc -l
5

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对华域联盟的支持。如果你想了解更多相关内容请查看下面相关链接

您可能感兴趣的文章:

本文由 华域联盟 原创撰写:华域联盟 » Shell中统计字符串中单词的个数的几种方法

转载请保留出处和原文链接:https://www.cnhackhy.com/19185.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部