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
4 comments:
Number 1 Google hit for "linux print file with line numbers". Thanks!
This page was a life saver to me. Great work.
Another way to number using awk:
awk '{print NR,$0}' myfile
I was trying to think up other ways this morning, and came up with 3 that aren't on your list:
printf 'set number\ng/^/p\n' | ex input.txt
echo '1,$n' | ed -s input.txt
sed -n '/^/{=;p}' input.txt | paste - - -d" "
If your ed doesn't have -s, add a | tail +2 or tail -n +2.
Post a Comment