How Do I Add Line Numbers to End of Each Line? (useful to convert a table of contents to an alphabetical index)

I don’t know where the text is when you start, but if it’s on the clipboard, a shell script of

pbpaste | perl -nle 'print "$_ $."' | pbcopy

will replace the unnumbered text with numbered text on the clipboard. If you expect to have a trailing linefeed at the end of the text and you don’t want that last blank “line” numbered, change the script to this:

pbpaste | perl -nle 'print "$_ $." unless (eof && !$_)' | pbcopy
2 Likes