← All Exercises

Top Words with tr, sort and uniq

basicbash~20min

Background

Word counts are a simple way to profile documents or debug tokenization. A streaming pipeline handles files larger than memory without extra tooling.

Problem

Given a text file and an optional limit, print the most frequent words of length three or more, ignoring a small stop-word list, one word and count per line.

Input

A readable text file and an optional positive integer limit, defaulting to 3, for how many words to return.

Output

Up to limit lines of word and count, ordered by count descending then word ascending; words are lowercase and contain only letters and digits.

Sample tests

Sample 1

Input: text: The cat and the dog. The cat runs. with limit 3
Expected: cat 2
dog 1
runs 1

Sample 2

Input: the same text with limit 1
Expected: cat 2

Your solution

Learning objective

Build a streaming text pipeline and control ordering with sort keys instead of relying on command defaults.