Tips for the nl command
nl is surprisingly versatile and a good choice if you need to add a unique ID field to a data table (as described here).
(See also this BASHing data post.)
Add line numbers
The nl command numbers the lines in a text file, or in standard input:
$ echo -e "aaa\nbbb\nccc"
$ echo -e "aaa\nbbb\nccc" | nl
Notice above that the line numbers look indented. You can think of the numbering as a set of columns, with the right-most column holding the line number, and the other columns blank. As the numbers get bigger, they occupy more columns to the left ('23' occupies 2 columns, '147' occupies 3 columns, etc). In other words, the line numbering is right-justified. The blank columns can be filled with leading zeroes using the '-nrz' option:
$ echo -e "aaa\nbbb\nccc" | nl -nrz
The default number of columns used by nl is 6, which allows for line numbering from 000001 to 999999. The '-w' option sets the number of columns. With 4 columns you could number lines from 0001 to 9999:
$ echo -e "aaa\nbbb\nccc" | nl -nrz -w4
If you don't want leading zeroes, you can avoid indentation by setting '-w' to 1 column. When line numbers get bigger than 9, the numbers will automatically be left-justified.
Remove line numbers
By default, nl separates line numbers from line text with one tab character. Since the cut command uses tabs as default field separators, you can remove that first, tab-separated field with cut:
You can control how nl separates line numbers from line text by setting the '-s' option to any string, such as 2 tabs:
$ echo -e "aaa\nbbb\nccc" | nl -w1 -s$'\t'$'\t'
or a word with leading and trailing spaces:
$ echo -e "aaa\nbbb\nccc" | nl -w1 -s" foo "
1 foo aaa
2 foo bbb
3 foo ccc
or a numbering convention, like a round bracket and a space:
$ echo -e "aaa\nbbb\nccc" | nl -w1 -s") "
or even a linefeed character, which will put the line number on a new line before the line it numbers:
$ echo -e "aaa\nbbb\nccc" | nl -w1 -s$'\n'
You can also control how nl does its numbering. The '-v' option sets the starting number (the default is 1) and the '-i' option sets the increment (the default is 1):
$ echo -e "aaa\nbbb\nccc" | nl -w1 -v0 -i2
An alternative to nl for numbering lines is the cat command with its '-n' option, but cat -n only does the default nl numbering, with 6 columns, numbers right-justified and a tab between the line number and the line text.