๐Ÿ“š General & Other

Reverse Numeric Sort and the Header Row Problem

sorts by word count descending. Two practical notes.

awk '{print NF"\t"$0}' | sort -rn | cut -f2- sorts by word count descending. Two practical notes.

Headers sort with the data

A header row has its own field count and lands somewhere in the middle of the output. Strip it before sorting and reattach afterwards, or use sort's header handling where available. A header buried in sorted output is easy to miss and corrupts anything that reads the file positionally.

-rn versus -nr

They are equivalent โ€” flag order does not matter โ€” but -r alone reverses a lexical sort, which is a different result entirely. The bug is omitting -n, not the order of the letters.

Field counting depends on the separator

NF counts whitespace-separated fields by default. For comma-separated data, awk -F, changes the count to comma-separated fields, which is a different number. Be explicit about which definition of "word" the sort is using, particularly if the result will be compared against a count produced elsewhere.

Try it: Sort by Word Count (Most First) on SeoWolf's Notepad.