home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 25.19 Reverse Lines in Long Files with flip Chapter 25
Showing What's in a File
Next: 25.21 Numbering Lines
 

25.20 Printing the Top of a File

Many versions of BSD UNIX include a nice program called head that prints the top n (default: 10) lines of a file. System V or other users without head can emulate its behavior with sed .

The easiest way is simply to use sed 's q command ( 34.21 ) :

% 

sed 10q 



file

head
If you want to get fancy, you can use a shell script to emulate all of the behavior of the BSD head command, including taking an option for the number of lines to be printed, and printing a separator line if multiple filenames are specified on the same command line.

The CD-ROM has that script. Most of it is straightforward. One interesting part is shown below. It's the sed command that prints the separator when more than one file is shown:

sed "
1i\\
==> $1 <==
${show}q" $1

The sed command 1i inserts the separator before line 1. The sed command q quits after the number of lines (by default, 10) in the $show shell variable ( 6.8 ) . The shell substitutes $1 with the filename being read. The double quotes ( " ) around the sed commands let the shell build the commands on-the-fly before starting sed .

- JP , TOR


Previous: 25.19 Reverse Lines in Long Files with flip UNIX Power Tools Next: 25.21 Numbering Lines
25.19 Reverse Lines in Long Files with flip Book Index 25.21 Numbering Lines

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










??????????????@Mail.ru