我尝试计算Bash中我的文件中的数字和字母的数量。 我知道我可以使用wc -c file
来计算字符的数量,但是如何将它修复为仅字母和其次数字?如何计算文件中的数字/字母数量?
2
A
回答
0
通过组合-c
(补充)和-d
(删除)标志,可以使用tr
仅保留字母数字字符。从那里,它只是一个部分管道的问题:
$ cat myfile.txr | tr -cd [:alnum:] | wc -c
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
您可以使用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
,punct
和whitespace
的编号。
#!/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
相关问题
- 1. 计算文件中字的数量
- 2. 如何计算NSString对象中非字母数字字符的数量?
- 3. 计算数据中字母字符的数量
- 4. 计算数组中的字母字符?
- 5. 如何计算来自队列中单词的字母数量?
- 6. Java计算字母数
- 7. 字母计数算法
- 8. Java:计算文本文件中的字母数
- 9. 如何计算文件中唯一字符的数量?
- 10. 如何计算excel中的字母数字元素?
- 11. 如何计算随机字符串中的字母数?
- 12. 如何计算字符串中的字母数?
- 13. 如何计算字母出现在字符串中的次数?
- 14. 计数字母文字
- 15. 只计算数字和字母列表中的数字
- 16. 如何计算Ruby中字符串之间不同字母的数量?
- 17. 如何计算包含在字母数字序列中的数字?
- 18. 如何计算文本文件中的大写字母?
- 19. 计算文本文件中的行数,字数和字符数
- 20. 计算文件中字母的出现次数
- 21. 猪脚本来计算文件中的字母数
- 22. 如何计算JavaScript中的字母?
- 23. 计算某个特定数字在csv文件中的数量
- 24. 字母和数字在计算机中的位数
- 25. 为什么ftell不能正确计算文件中字母的数量?
- 26. 协助计算文件中的数字
- 27. 计算文件中的指定字数
- 28. Python - 计算文件中的字符数
- 29. 计算文件中的字符数C#
- 30. 计算文件/流中的字符数
'猫myfile.txr | tr -cd [123456789] | wc -c'那个例子是正确的? –
猫的无用用途。如果有一个名为'm'的文件则失败。 – Jens
@ K.Dote 0-9不是1-9 –