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


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 33.2 Writing Editing Scripts Chapter 33
Batch Editing
Next: 33.4 Useful ex Commands
 

33.3 Line Addressing

The key to making line editors work for you is understanding how to select (or "address") the lines that will be affected by the commands in your script.

In ed and ex , a command affects only the "current" line - the first line of the file to begin with, and later the site of the last edit or movement command - unless you precede the command with an address to indicate some other line or lines. In sed , most commands apply to every line unless you give an address.

Most line editors address lines in three ways:

  • with line numbers

  • with regular expression patterns

  • with special symbols

It's possible to address single lines or a range of lines.

describes the addresses you can use with ex .

Table 33.1: Line Addressing in the ex Editor
1,$ All lines in the file.
% All lines; same as 1,$ .
x , y Lines x through y .
x ; y Lines x through y , with current line reset to x .
1 Top of file.
0 "Before the top" of file. Used to add text above top line: 0r , x m0 , etc.
. Current line.
n Absolute line number n .
$ Last line.
x - n n lines before x .
x + n n lines after x .
- n n lines previous.
- Previous line.
+ n n lines ahead.
+ Next line.
' x Line marked with x . (To mark a line, use k x .)
'' Previous mark.
/ pattern / Next line matching pattern .
? pattern ? Previous line matching pattern .

If the address specifies a range of lines, the format is:



x


,


y

where x and y are the first and last addressed lines. x must precede y in the file.

- TOR , DG, JP


Previous: 33.2 Writing Editing Scripts UNIX Power Tools Next: 33.4 Useful ex Commands
33.2 Writing Editing Scripts Book Index 33.4 Useful ex Commands

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