user2999200
user2999200

Reputation: 159

Average word length of input file

If i use

wc -m filename 

it will generate the number of characters

and

wc -w filename

will generate number of words

if i used this info by dividing number of characters/number of words

it will give me misleading result as number of character will include spaces and punctuation

any advice ?

Upvotes: 1

Views: 267

Answers (2)

jim mcnamara
jim mcnamara

Reputation: 16379

Subtract out characters you do not want

 chars=$(tr -dc '[:alnum:]' < filename | wc -c)
 words=$(cat filename | wc -c)

Now do you calculation. I piped into wc to avoid the extra "filename" in output

printf "%.2f" $(echo "$chars/$words" | bc -l)

Edit: thanks BMW

Upvotes: 2

DGB
DGB

Reputation: 253

the solution that I came up with without writing a script was to pipe it through a couple of commands like this.

<filename  tr -d " \t\n\r\.\?\!" | wc -m

This works to remove all of the spacing, like new line, tabs and normal spacing. A more rigorous tr command that included any sort of other punctuation like a colon can just be added to the list for example \:

Hope That Helps

Upvotes: 3

Related Questions