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

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 34.20 The sed Test Command Chapter 34
The sed Stream Editor
Next: 34.22 Dangers of the sed Quit Command
 

34.21 Uses of the sed Quit Command

The quit command, q , causes sed to stop reading new input lines (and stop sending them to the output). Its syntax is:

[line-address ]q

Warning! It can take only a single-line address. Once the line matching address (line-address ) is reached, the script will be terminated.

For instance, the following one-liner uses the quit command to print the first ten lines from a file:

% sed '10q' myfile


   ...

sed prints each line until it gets to line 10 and quits.

The previous version is much more efficient than its functional equivalent:

-n
 
% sed -n '1,10p' myfile

(especially if myfile is a long file) because sed doesn't need to keep reading its input once the patterns in the script are satisfied.

One possible use of q is to quit a script after you've extracted what you want from a file. There is some inefficiency in continuing to scan through a large file after sed has found what it is looking for. The getmac (43.20 ) script uses this technique.

- TOR


Previous: 34.20 The sed Test Command UNIX Power Tools Next: 34.22 Dangers of the sed Quit Command
34.20 The sed Test Command Book Index 34.22 Dangers of the sed Quit Command

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










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