How to count the number of words of each sentences in a file using shell command?

7,752

Solution 1

You can do it with:

awk '{print NF}' filename

Solution 2

Since you tagged your question with wc, here's a solution (albeit non-optimal) that uses it:

while read -r line; do echo $line | wc -w; done < filename.txt
Share:
7,752

Related videos on Youtube

Nainita
Author by

Nainita

Updated on September 18, 2022

Comments

  • Nainita
    Nainita over 1 year

    I have text files like this:

    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
    

    All files have only four lines

    Only the count of words must be printed as an output, just like this:

    8
    10
    9
    4
    
  • Pablo A
    Pablo A over 2 years
    FTR, NF (number of fields) works because awk uses space as field separator (FS) by default.