简体   繁体   English

如何在不使用任何外部编程语言(awk等)的情况下以bash打印最常见的单词

[英]How can I print the most commonly occuring words in bash without using any external programming language (awk etc))

I want to print the most frequently occurring words in a text file in descending order, but I cant use awk , or sed , and it has to be a bash one-liner 我想按降序打印文本文件中出现频率最高的单词,但是我不能使用awksed ,它必须是bash一线笔

I can print the most frequently occurring words/numbers, however I just want to print the words and not the count 我可以打印最常出现的单词/数字,但是我只想打印单词而不是计数

sort inpt.txt | uniq -c | sort -nr | head -n 5 

So it gives something like 所以它给像

   24 q
   13 3
    6 Y
    4 g
    3 N

however I just want 但是我只想要

q
3
Y
g
N

您可以切掉不需要的部分

sort inpt.txt | uniq -c | sort -nr | head -n 5 | cut -b7-

You can use cut -c <range> to extract the text based on column numbers in the output: 您可以使用cut -c <range>根据输出中的列号提取文本:

sort inpt.txt | uniq -c | sort -nr | head -n 5 | cut -c 9-

outputs what you want on my machine. 在我的机器上输出您想要的内容。

You might have to tweak the 9- range on your machine if it's not picking up exactly the right columns. 如果未正确选择正确的列,则可能需要调整计算机上的9-范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM