3

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

2 Answers 2

5

You can do it with:

awk '{print NF}' filename

1
  • FTR, NF (number of fields) works because awk uses space as field separator (FS) by default.
    – Pablo A
    Jan 13, 2022 at 23:44
3

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .