Search This Blog

Loading...

Tuesday, June 3, 2008

How to number each line in a text file on Linux

Some Linux commands support options that will number the input lines as a side effect, e.g., grep, and cat. The nl command is on the other hand dedicated to the task of numbering lines in a text file. If you want maximum flexibility, sed or perl is your best bet.

Suppose you want to number the lines in the input.txt file which contains:
123

456

789


abc

def

ghi


Below are some ways to number input.txt:

  • cat -n
    $ cat -n input.txt
    1 123
    2
    3 456
    4
    5 789
    6
    7
    8 abc
    9
    10 def
    11
    12 ghi


  • grep -n
    $ grep -n '^' input.txt
    1:123
    2:
    3:456
    4:
    5:789
    6:
    7:
    8:abc
    9:
    10:def
    11:
    12:ghi



  • nl

nl inserts the line number at the beginning of each non-empty line. By default, the line number is 6 characters wide, right justified with leading spaces. A tab is inserted by default after the line number.
$ nl input.txt
1 123

2 456

3 789


4 abc

5 def

6 ghi


If your file is small, 6 is perhaps too wide for the line number field. To adjust the width of the line number, use the -w option. To make nl number all lines including blank ones, add -ba option.
$ nl -ba -w 3 input.txt
1 123
2
3 456
4
5 789
6
7
8 abc
9
10 def
11
12 ghi


If you don't want a tab after the line number, you can replace the tab with null (no separator between line number and rest of line), or any string you want. Use -s '' for no separator or -s ' ' for a space.
$ nl -ba -w 3  -s ' ' input.txt
1 123
2
3 456
4
5 789
6
7
8 abc
9
10 def
11
12 ghi


If you prefer left justifying the line numbers, set the field width to 1 (or use -n ln option).

$ nl -ba -w 1  input.txt
1 123
2
3 456
4
5 789
6
7
8 abc
9
10 def
11
12 ghi



nl is flexible enough to only number lines that match a regular expression. This is done by specifying the option -b p followed by a regular expression on the command line.

Number only those lines that start with either the character 1 or 4.

$ nl -b 'p^[14]' -w 3 -s ' ' input.txt
1 123

2 456

789


abc

def

ghi


Note that we specify -s ' ' to use a single space as the separator between line number and body text. This is used to preserve text alignment (the default tab will cause output to look messy).

Number only those lines that contain the "words" 12 or ef.
$ nl -b 'p12\|ef' -w 3 -s ' ' input.txt
1 123

456

789


abc

2 def

ghi

3 comments:

Anonymous said...

Number 1 Google hit for "linux print file with line numbers". Thanks!

satishgoda said...

This page was a life saver to me. Great work.

Anonymous said...

Another way to number using awk:

awk '{print NR,$0}' myfile