3

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?

4

2 回答 2

4

这应该可以解决问题

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

或者,如果您想在空白定义中包含制表符

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

根据 OP 的评论,删除字母、空格、数字和换行符并计算所有剩余字符:

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

[:alnum:]占字母和数字的字母。[:space:]处理所有空格,包括换行符

于 2014-02-25T03:05:38.960 回答
1

只是在这里发布以供参考,如果您希望一次性完成所有操作,那么此awk脚本应该可以工作:

awk -v FS='' '
{
    for(i=1; i<=NF; i++) {
        if($i ~ /[a-zA-Z]/) {alpha++};
        if($i == " ") {space++};
        if($i !~ /[A-Za-z0-9 ]/) {spl++}
    }
}
END {
    printf "Space=%s, Alphabets=%s, SplChars=%s, NewLines=%s\n", space, alpha, spl, NR
}' file

$ cat file
This is a text
I want to count
alot of $tuff
in 1 single shot

$ awk -v FS='' '
{
    for(i=1; i<=NF; i++) {
        if($i ~ /[a-zA-Z]/) {alpha++};
        if($i == " ") {space++};
        if($i !~ /[A-Za-z0-9 ]/) {spl++}
    }
}
END {
    printf "Space=%s, Alphabets=%s, SplChars=%s, NewLines=%s\n", space, alpha, spl, NR
}' file
Space=11, Alphabets=45, SplChars=1, NewLines=4
于 2014-02-25T05:56:03.350 回答