APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS














(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



Numbering Lines




2006/11/15

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

#!/bin/bash
x=1;
while read line
do
echo -n "$x: "
echo $line
x=$((x+1))
done



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
5
6 echo -n "$x: "
7 echo $line
8 x=$((x+1))
9
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)"


;


Click here to add your comments





Sun Mar 9 23:57:14 2008: Subject:   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: Subject:   TonyLawrence

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



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-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. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server