在shell脚本中计算文件中的字母
我需要一个shell脚本/powershell,它可以计算文件中类似的字母 输入:在shell脚本中计算文件中的字母,shell,powershell,scripting,Shell,Powershell,Scripting,我需要一个shell脚本/powershell,它可以计算文件中类似的字母 输入: this is the sample of this script. This script counts similar letters. 输出: t 9 h 4 i 8 s 10 e 4 a 2 ... 10 s 9 t 8 i 4 r 4 h 4 e 3 p 3 l 3 c 2 o 2 m 2 a 1 u 1 n 1 f 这一班轮应能: awk
this is the sample of this script.
This script counts similar letters.
输出:
t 9
h 4
i 8
s 10
e 4
a 2
...
10 s
9 t
8 i
4 r
4 h
4 e
3 p
3 l
3 c
2 o
2 m
2 a
1 u
1 n
1 f
这一班轮应能:
awk 'BEGIN{FS=""}{for(i=1;i<=NF;i++)if(tolower($i)~/[a-z]/)a[tolower($i)]++}
END{for(x in a)print x, a[x]}' file
在PowerShell中,可以使用cmdlet执行此操作: 这是我的机器上的输出,带有示例输入+换行符 powershell一级衬里:
"this is the sample of this script".ToCharArray() | group -NoElement | sort Count -Descending | where Name -NE ' '
输出、排序、最常用字母优先:
10 s
10
8 t
8 i
4 r
4 h
4 e
3 p
3 l
3 c
2 o
2 m
2 a
2 .
1 u
1 T
1 n
1 f
注:无需
sed
或awk
;一个简单的grep-o'.
完成所有繁重的工作。要不计算空格和标点符号,请将'.
替换为'[[:alpha:].
:
echo "this is the sample of this script. \
This script counts similar letters." | \
grep -o '[[:alpha:]]' | sort | uniq -c | sort -rg
要将大写字母和小写字母计算为一个,请使用排序
和uniq
的--忽略大小写
选项:
echo "this is the sample of this script. \
This script counts similar letters." | \
grep -o '[[:alpha:]]' | sort -i | uniq -ic | sort -rg
输出:
t 9
h 4
i 8
s 10
e 4
a 2
...
10 s
9 t
8 i
4 r
4 h
4 e
3 p
3 l
3 c
2 o
2 m
2 a
1 u
1 n
1 f
我会在排序之前将过滤器向上移动(无需对要丢弃的内容进行排序)@aphoria我看到
shell脚本/powershell
如果将行保存在文件中,那么它就是shell脚本。还有一个问题用shell
在标签上移动鼠标,说明会显示出来。再说一遍,我没有否决你,所以你不必向我解释……我只是在猜测为什么其他人可能否决了你。另一个不是PowerShell解决方案的答案也被否决。我不是说你应该被否决,这只是一个可能的原因。谢谢,但它在PS1格式中是什么样子的?我需要这样的输入:task.ps1字母。txt@MolnárBence删除函数计数字母{}
块,这样task.ps1
文件中的第一行就是参数(
打开-然后您可以按照您所描述的方式调用它。如果您不希望outputi删除块顶部的header+分隔符,请将其导入Format Table-HideTableHeaders
,但它不起作用。您可以看到问题:@MolnárBence作为错误状态,它与代码本身无关,但与执行策略无关在您的计算机上设置。如果您发现自己无法使用Set ExecutionPolicy
更改它,请尝试像这样启动PowerShell:PowerShell.exe-ExecutionPolicy bypass
,然后从那里尝试
echo "this is the sample of this script. \
This script counts similar letters." | \
grep -o '[[:alpha:]]' | sort -i | uniq -ic | sort -rg
10 s
9 t
8 i
4 r
4 h
4 e
3 p
3 l
3 c
2 o
2 m
2 a
1 u
1 n
1 f