Search code examples
bashtrwc

I'm trying to use tr with multiple sets and not sure how


I have used:

tr -dc [:alpha:] < $fileDoc | wc -c

to count all letters,

tr -dc ' ' < $fileDoc | wc -c

to count all spaces,

tr -dc '\n' < $fileDoc | wc -c

to count all new lines in a text document.

What I would like to do now is to do now is count all other characters in the document as I will call every thing else.

Here is the text from the document:

Hello this is a test text document.

123

!@#

Is there a way to delete everything [:alpha:], , and \n found and count the remaining characters?


Solution

  • This should do the trick

    tr -d '[:alpha:] \n'  < $fileDoc | wc -c
    

    Or perhaps if you want to include tabs in the definition of blanks

    tr -d '[:alpha:][:space:]'  < $fileDoc | wc -c
    

    Based on the OP's comment, to delete alphabetical, spaces, digits, and newlines and count all remaining characters:

    tr -d '[:alnum:][:space:]' < $fileDoc | wc -c
    

    [:alnum:] accounts for letters of the alphabet and digits. [:space:] takes care of all whitespace including newlines