-4

Exactly what the title says: what is the bash command for printing out the longest word in a text file that appears at least 10 times.

  • Hi Denis, you should give your homework assignment a try yourself. If you provide sample input, a program that attempts to get the results you are looking for, and sample output, the folks here will try to help you with your programming questions. I found a little code here that breaks up the input file into words: https://stackoverflow.com/questions/25158710/sed-remove-whole-words-containg-a-character-class/25158757#25158757 . If you change the accepted answer program a little, you can have the awk program print out all the words and their lengths. Then sort and count. – Mark Mar 04 '20 at 01:48

1 Answers1

1

Try this Denis:

tr -s " " "\n" < file | while read -r l; do echo "${#l} $l"; done | sort -n | awk '$1 >= 10 ' | awk '{print $2}' | tail -n1
jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • This only prints out the longest word. – Denis Shevchenko Mar 04 '20 at 23:29
  • Yes, it prints the longest word that appears at least 10 times. ("what is the bash command for _printing out the longest word in a text file that appears at least 10 times_"). If you want the list of words that appear at least 10 times, remove the `| tail -n1` from the end – jared_mamrot Mar 04 '20 at 23:45
  • I tested this bash script on a few files. What awk '$1 >= 10 ' seems to be doing is finding word that have the length of at least ten. When I removed tail -n1, it printed out all the words of at least that length. – Denis Shevchenko Mar 04 '20 at 23:57