๐Ÿ“š General & Other

Numbering Lines and Restoring an Original Order

numbers lines; does too, with different defaults. The interesting use is not display but preserving order through operations that destroy it.

nl numbers lines; cat -n does too, with different defaults. The interesting use is not display but preserving order through operations that destroy it.

The sort-and-restore pattern

Number the lines, perform whatever reordering you need, then sort numerically on the prefix to return to the original sequence. This is how you apply a sort-based operation to data whose order is meaningful.

nl skips blank lines by default

nl numbers only non-empty lines unless told otherwise with -ba. If the line numbers are being used as positional identifiers, that default silently misaligns them against the original file. cat -n numbers everything.

Zero-padding for lexical sorts

If the numbered output will be sorted as text rather than numerically, 10 sorts before 9. Pad to a fixed width โ€” nl -nrz -w6 โ€” so lexical and numeric order agree. This matters most when the numbers become filenames, where the sort is almost always lexical.

Try it: Number Lines on SeoWolf's Notepad.