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


Unix Power ToolsUnix Power ToolsSearch this book

17.8. Using Search Patterns and Global Commands

Besides using line numbers and address symbols (., $, %), ex (including the ex mode of vi, of course) can address lines (Section 20.3) using search patterns (Section 32.1). For example:

:/pattern/d
Deletes the next line containing pattern.

:/pattern/+d
Deletes the line below the next line containing pattern. (You could also use +1 instead of + alone.)

:/pattern1/,/pattern2/d
Deletes from the next line (after the current line) that contains pattern1 through the next following line that contains pattern2.

:.,/pattern/m23
Takes text from current line (.) through the next line containing pattern and puts it after line 23.

Note that patterns are delimited by a slash both before and after.

If you make deletions by pattern with vi and ex, there is a difference in the way the two editors operate. Suppose you have in your file named practice the following lines:

With a screen editor you can scroll the
page, move the cursor, delete lines, insert
characters and more, while seeing results
of your edits as you make them.

Key-strokes

Action

Results

d/while

The vi delete-to-pattern command deletes from the cursor up to the word while but leaves the remainder of both lines.

With a screen editor you can scroll the
page, move the cursor, while seeing results
of your edits as you make them.

:.,/while/d

The ex command deletes the entire range of addressed lines; in this case both the current line and the line containing the pattern. All lines are deleted in their entirety.

With a screen editor you can scroll the
of your edits as you make them.



Library Navigation Links

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