2016-05-15 122 views

回答

0

通过组合-c(补充)和-d(删除)标志,可以使用tr仅保留字母数字字符。从那里,它只是一个部分管道的问题:

$ cat myfile.txr | tr -cd [:alnum:] | wc -c 
+0

'猫myfile.txr | tr -cd [123456789] | wc -c'那个例子是正确的? –

+0

猫的无用用途。如果有一个名为'm'的文件则失败。 – Jens

+0

@ K.Dote 0-9不是1-9 –

0

要算你可以wc结合grep的字母和数字编号:

grep -o [a-z] myfile | wc -c 
grep -o [0-9] myfile | wc -c 

有了调整,你可以修改它的点点数数字或字母字或字母词是这样,

grep -o [a-z]+ myfile | wc -c 
grep -o [0-9]+ myfile | wc -c 
grep -o [[:alnum:]]+ myfile | wc -c 
+0

终端显示来自第一个和第二个示例的错误输出,嗯? –

+0

这将对至少包含*一个字母或数字字符的任何行的所有字符进行计数。 – Jens

+0

使用'grep -o'来指定'混合线111'。 –

0

您可以使用SED来替换是那种不是所有字符你正在寻找,然后字数结果的字符。

# 1h;1!H will place all lines into the buffer that way you can replace 
# newline characters 
sed -n '1h;1!H;${;g;s/[^a-zA-Z]//g;p;}' myfile | wc -c 

It's easy enough to just do numbers as well. 
sed -n '1h;1!H;${;g;s/[^0-9]//g;p;}' myfile | wc -c 

Or why not both. 
sed -n '1h;1!H;${;g;s/[^0-9a-zA-Z]//g;p;}' myfile | wc -c 
0

有许多的方式来处理分析线,并在bash的文本文件的性格频率。利用bash内建字符大小写筛选器(例如[:upper:]等),您可以深入查看文本文件中每种字符类型出现的频率。下面是一个简单的脚本,它从stdin中读取并提供正常的wc输出作为其第一行输出,然后输出upper,lower,digits,punctwhitespace的编号。

#!/bin/bash 

declare -i lines=0 
declare -i words=0 
declare -i chars=0 
declare -i upper=0 
declare -i lower=0 
declare -i digit=0 
declare -i punct=0 

oifs="$IFS" 

# Read line with new IFS, preserve whitespace 
while IFS=$'\n' read -r line; do 

    # parse line into words with original IFS 
    IFS=$oifs 
    set -- $line 
    IFS=$'\n' 

    # Add up lines, words, chars, upper, lower, digit 
    lines=$((lines + 1)) 
    words=$((words + $#)) 
    chars=$((chars + ${#line} + 1)) 
    for ((i = 0; i < ${#line}; i++)); do 
     [[ ${line:$((i)):1} =~ [[:upper:]] ]] && ((upper++)) 
     [[ ${line:$((i)):1} =~ [[:lower:]] ]] && ((lower++)) 
     [[ ${line:$((i)):1} =~ [[:digit:]] ]] && ((digit++)) 
     [[ ${line:$((i)):1} =~ [[:punct:]] ]] && ((punct++)) 
    done 
done 

echo " $lines $words $chars $file" 
echo " upper: $upper, lower: $lower, digit: $digit, punct: $punct, \ 
whitespace: $((chars-upper-lower-digit-punct))" 

测试输入

$ cat dat/captnjackn.txt 
This is a tale 
Of Captain Jack Sparrow 
A Pirate So Brave 
On the Seven Seas. 
(along with 2357 other pirates) 

示例使用/输出

$ bash wcount3.sh <dat/captnjackn.txt 
5 21 108 
upper: 12, lower: 68, digit: 4, punct: 3, whitespace: 21 

您可以自定义脚本,只要你喜欢,给你很少或尽可能多的细节。如果您有任何问题,请告诉我。

1

这里是完全避免了管道,只是用tr和外壳的方式给一个变量与${#variable}长度的方式:

$ cat file 
123 sdf 
231 (3) 
huh? 564 
242 wr =! 
$ NUMBERS=$(tr -dc '[:digit:]' < file) 
$ LETTERS=$(tr -dc '[:alpha:]' < file) 
$ ALNUM=$(tr -dc '[:alnum:]' < file) 
$ echo ${#NUMBERS} ${#LETTERS} ${#ALNUM} 
13 8 21