bashshellsortingwc

How to get the average number of words in files using the output of "wc -w"


I'm listing the number of words in a bunch of files and sorting it like this:

wc -w *.tex | sort -rn

which outputs a nice list of the files and word count for each file

   17423 total
    6481 panama-to-colombia.tex
    5516 the-salt-flats.tex
    5426 hiking-cordillera-huayhuash.tex

How can I also calculate and display the average number of words per file? i.e. a line at the bottom like:

5808 AVERAGE

Note: I'd like to find a solution that works for an arbitrary number of files in the list.


Solution

  • I suggest to append to your code:

    | awk '{sum=sum+$1; print};END{print sum/2/(NR-1),"AVERAGE"}'
    

    sum=sum+$1 adds the number in the first column ($1) to the variable sum in each row. print outputs the current row unchanged. The average is calculated after the last line read in. During the calculation, please note that the line with total is also included in the output of wc -w *.tex.


    See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR