Bash脚本查找文件中每个字母的频率 [英] Bash script to find the frequency of every letter in a file
本文介绍了Bash脚本查找文件中每个字母的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试查找输入文件中英文字母中每个字母的出现频率.如何在bash脚本中执行此操作?
I am trying to find out the frequency of appearance of every letter in the english alphabet in an input file. How can I do this in a bash script?
推荐答案
一个awk命令
awk -vFS="" '{for(i=1;i<=NF;i++)w[$i]++}END{for(i in w) print i,w[i]}' file
如果不区分大小写,请添加tolower()
if you want case insensitive, add tolower()
awk -vFS="" '{for(i=1;i<=NF;i++)w[tolower($i)]++}END{for(i in w) print i,w[i]}' file
,如果只需要字符,
awk -vFS="" '{for(i=1;i<=NF;i++){ if($i~/[a-zA-Z]/) { w[tolower($i)]++} } }END{for(i in w) print i,w[i]}' file
,如果只需要数字,请将/[a-zA-Z]/
更改为/[0-9]/
and if you want only digits, change /[a-zA-Z]/
to /[0-9]/
如果您不想显示unicode,请执行export LC_ALL=C
if you do not want to show unicode, do export LC_ALL=C
这篇关于Bash脚本查找文件中每个字母的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文