Полезная информация

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 25.20 Printing the Top of a File Chapter 25
Showing What's in a File
Next: 26. Regular Expressions (Pattern Matching)
 

25.21 Numbering Lines

There are times when you want to print out a file with the lines numbered - perhaps because you are showing a script or program in documentation and want to refer to individual lines in the course of your discussion.

This is one of the handy things cat can do for you, with the -n option.

cat -n precedes each line with some leading spaces, the line number, and a TAB. How many leading spaces? It depends on how high the line numbers go. The line numbers are right justified at column 6, which means that a six-digit number will go all the way back to the margin. I only belabor this point in case you're tempted to trim the leading spaces with something like cut (35.14).

If you have a version of cat that doesn't support -n, try nl, the line-numbering program. nl -ba acts like cat -n. By itself, nl numbers only lines with text.

You can achieve a similar effect with pr -t -n. (The -t keeps pr from inserting the header and footer (43.7) it normally uses to break its output into pages.) And as long as we're giving you choices, here are four more :-) from Greg Ubben:

grep -n \^
awk '{print NR,$0}'
sed = | sed 'N;s/\n/ /'
ex - '+%#q'

- JP, TOR


Previous: 25.20 Printing the Top of a File UNIX Power ToolsNext: 26. Regular Expressions (Pattern Matching)
25.20 Printing the Top of a File Book Index26. Regular Expressions (Pattern Matching)

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System