2016-05-15 97 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