APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Numbering Lines


Now and then you want to number the lines of a file. You can roll your own script to do that:

while read line
echo -n "$x: "
echo $line

If you run that on itself ("thescript < thescript"), you get:

1: #!/bin/bash
2: x=1;
3: while read line
4: do
5: echo -n "$x: "
6: echo $line
7: x=$((x+1))
8: done

You get something very similar to that with "nl thescript":

$ nl thescript
1 #!/bin/bash
2 x=1;
3 while read line
4 do
5 echo -n "$x: "
6 echo $line
7 x=$((x+1))
8 done

On most systems, "cat -n" works just like "nl", but "nl" has more tricks up its sleeves. If we add some blank lines to our file, one difference becomes obvious:

$ nl thescript
1 #!/bin/bash
2 x=1;
3 while read line
4 do

5 echo -n "$x: "
6 echo $line
7 x=$((x+1))

8 done
$ cat -n thescript

1 #!/bin/bash
2 x=1;
3 while read line
4 do
6 echo -n "$x: "
7 echo $line
8 x=$((x+1))
10 done

"nl" has flags to turn on numbering for blank lines and to use a different increment. It can restart numbering at each new page (or not), or number only those lines containing certain text ('nl -b pecho thescript'). Check the man page; nl is a powerful little tool.

Several other tools have switches to add line numbering: pr, a2ps and enscript are a few I can think of immediately, but there are surely many more. Word processors have this too; in Microsoft Word it's under Page Layout. Even crappy old MSDOS apparently can do that.

Let's not forget 'awk':

awk '{printf "%3d: %s\n", NR, $0}' thescript

Very often I just want to know about line numbers while in "vi". There are two ways to do that: ":.=" (colen period equal) will tell you the number of the current line, and ":set number" will turn on numbering ahead of each line (the numbers aren't written out when you save the file). Turn numbering off with ":set nonumber". Of course emacs has similar features.

By the way: I never use emacs voluntarily, so when I am forced to, I can never remember how to get out of the darn thing. People who never use vi probably have a similar problem. For vi, hit ESCAPE, then ":q!(ENTER)" to get out without writing. If you DO want to write, use ":wq(ENTER)". To exit emacs, use CTRL-X CTRL-C. If you are stuck in some Emacs command (again: I HATE Emacs!), try Ctrl-G a couple of times. For "ed", just type "q!(ENTER)", or "wq(ENTER)" if you want to write. If those don't work, just type enter and then ".(ENTER)"

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Numbering lines of a file:


Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Anthony Lawrence

Sun Mar 9 23:57:14 2008: 3825   anonymous

there's one thing that's different about line numbering in microsoft word:
it doesn't number based on newline, CR, LF, or the like.
it numbers based on the margins of the page.
i am trying to find a way to replicate this in unix.
any ideas?

Mon Mar 10 11:10:22 2008: 3826   TonyLawrence

You just make the physical format match the margins you want, using "fmt" or whatever.

Kerio Samepage

Have you tried Searching this site?

Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us