时间:2021-04-18 09:52:36 | 栏目: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
总结