(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Kerio Reseller
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


Hate these ads?



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)"



Comments /Linux/line_numbers.html


Sun Mar 9 23:57:15 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


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



Add your comments

Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)

Or use any RSS reader

Delivered by FeedBurner


Views for this page
Today This Week This Month This Year  Overall
111601,397 4,329

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

pavatar.jpg
More:
       - Shell
       - Linux
       - Unix
       - MacOSX




Related Posts

Death of the command line

Bash history control

Why use the command line?

Clever Bash Tricks

iTerm is an alternative to Mac OS X Terminal.app

Using the shell in Mac OS X

MacOSX Skills Test

Printing file listings

Finding large files at the command line

Google Site search for "bashrc"

Shell Bashing

MacOSX Skills Test


Unix/Linux Consultants

Your ad here - $24.00 yearly!

SCO, OpenServer, UnixWare, software, servers, security, networks, installation, administration, troubleshooting, maintenance, Watchguard, firewalls, VPNs, e-mail. Visit us at http://opensystemscomputing.com and www.go2unix.com.


http://bcstechnology.net Full service Linux & UNIX systems integrator; Windows to UNIX/Linux Client-Server Specialist; Secure E-Mail & Website Hosting; Thoroughbred Software Developer; Custom Industrial Automation; Hardware & Electronics Experts; In Business Since 1985.


http://www.m3ipinc.com Security, firewalls, ids, audits, vulnerability assesments, BS7799, HIPAA, GLB, incident handling







Coming Attractions

My Favorites

Change Congress


Related Posts