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

Unix Power ToolsUnix Power ToolsSearch this book

20.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.

Table 20-1 describes the addresses you can use with ex.

Table 20-1. Line addressing in the ex editor




All lines in the file.


All lines; same as 1,$.


Lines x through y.


Lines x through y, with current line reset to x.


Top of file.


"Before the top" of file. Used to add text above top line: 0r, xm0, etc.


Current line.


Absolute line number n.


Last line.


n lines before x.


n lines after x.


n lines previous.


Previous line.


n lines ahead.

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


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

--TOR, DG, and JP

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.