如何使用shell命令统计文件中每个句子的单词数?

如何使用shell命令统计文件中每个句子的单词数?

我有这样的文本文件:

Mr.P.K.Baneerjee has visited the site today at 4.30pm
The permit tag has been set in the ds busbar
Opearation has been performed in presence of control engineer
Opeation was successfully completed

所有文件只有四行

只需打印单词数作为输出,就像这样:

8
10
9
4

答案1

你可以这样做:

awk '{print NF}' filename

答案2

由于您用 标记了您的问题wc,因此这里有一个使用它的解决方案(尽管不是最佳的):

while read -r line; do echo $line | wc -w; done < filename.txt

相关内容