简体   繁体   中英

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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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