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


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 30.8 Get Back What You Deleted with Numbered Buffers Chapter 30
vi Tips and Tricks
Next: 30.10 Confirming Substitutions in ex and vi
 

30.9 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 by using search patterns ( 26.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 practice the lines:

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

Keystrokes Results
d/while
 With a screen editor you can scroll the 
 page, move the cursor, 
w
hile seeing results  
 of your edits as you make them.

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

:.,/while/d
 With a screen editor you can scroll the 
 
o
f your edits as you make them.

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.

30.9.1 Global Searches

In vi you use a / (slash) to search for patterns of characters in your files. By contrast, ex has a global command, g , that lets you search for a pattern and display all lines containing the pattern when it finds them. The command :g! does the opposite of :g . Use :g! (or its synonym :v ) to search for all lines that do not contain pattern .

You can use the global command on all lines in the file, or you can use line addresses to limit a global search to specified lines or to a range of lines.

:g/ pattern /

Finds (moves to) the last occurrence of pattern in the file.

:g/ pattern /p

Finds and displays all lines in the file containing pattern .

:g!/ pattern /nu

Finds and displays all lines in the file that don't contain pattern ; also displays line number for each line found.

:60,124g/ pattern /p

Finds and displays any lines between lines 60 and 124 containing pattern .

g can also be used for global replacements. For example, to search for all lines that begin with WARNING: and change the first word not on those lines to NOT :



\<..\>
 

:g/^WARNING:/s/\<not\>/NOT/

- LL from O'Reilly & Associates' Learning the vi Editor , Chapter 5


Previous: 30.8 Get Back What You Deleted with Numbered Buffers UNIX Power Tools Next: 30.10 Confirming Substitutions in ex and vi
30.8 Get Back What You Deleted with Numbered Buffers Book Index 30.10 Confirming Substitutions in ex and vi

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