UNIX Hints & Hacks

ContentsIndex

Chapter 8: Editors

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

8.1 The Anatomy of ed & vi

 

8.5 Abbreviating vi Commands

 

 

8.2 The Six Steps to ed

 

8.6 Creating Macros

 

8.10 Edit, Run, and Edit Again

 

8.3 Six Simple Steps to vi

 

8.7 Search and Replace

 

8.11 Reading STDOUT into vi

 

8.4 Configuring vi Parameters

 

8.8 Other Places to Use vi

 

8.12 Using vi when tmp Is Full

 

 

8.7 Search and Replace

8.7.1 Description

8.7.1 Description

As in any editor there is always a need for searching for and replacing text and data. There are more ways than one to handle various situations.

Example One: Basic Search-and-Replace

Although you should already have an understanding of the basic search-and-replace, it is such a powerful tool when used properly that it had to be included briefly.

Syntax:

:[x,y]s/pattern1/pattern2

To search-and-replace the first occurrence of the word foo and replace it with the word foobar, on a single line, use the following:

:s/foo/foobar

To search-and-replace the first occurrence of the word foo on lines 1-10 and replace it with the word foobar, use the following:

:1,10s/foo/foobar

Syntax:

:[x,y]g/pattern1/s//pattern2/g

To search-and-replace all occurrences of the word foo and replace them with the word foobar, through the entire file, use the following:

:g/foo/s//foobar/g

To search-and-replace all occurrences of the word foo from line 15 to the end of the file and replace them with the word foobar, use the following:

:15,$g/foo/s//foobar/g

Example Two: Interactive

It is possible within vi to search interactively through a file. When a pattern is found, the editor prompts you to answer "y" or "n" if you want the pattern changed. When this method is used, you are prompted line by line from the bottom of the screen. There is no fullscreen interaction with the text. You are unable to see the lines surrounding the pattern in question.

Syntax:

:[x,y]g/pattern1/s//pattern2/gc

To search interactively ( c) for all occurrences of the pattern word foo and replace them with foobar through the entire file:

:g/foo/s//foobar/gc

Each word foo that is found is underscored with the caret ( ^) symbol and the editor waits for a "y" or "n" followed by the Enter key. It is the applying of the option c at the end that makes this work.

If you start with a file that consists of host entries

199.44.192.10   saturn.foo.com
199.44.192.11   jupiter.foo.com
199.44.192.12   uranus.foo.com

perform the interactive search and replace:

:g/foo/s//foobar/gc
199.44.192.10   saturn.foo.com
                      ^^^y<return>
199.44.192.11   jupiter.foo.com
                      ^^^n<return>
199.44.192.12   uranus.foo.com
                      ^^^y<return>

The results are a domain change in the first and third lines.

199.44.192.10   saturn.foobar.com
199.44.192.11   jupiter.foo.com
199.44.192.12   uranus.foobar.com

If you want to perform only a partial search of the file and not all the lines within it, you can apply a line count to the command:

:1,10g/NT/s//UNIX/gc

This command searches for all occurrences of the word NT and replaces it with the word UNIX in the first 10 lines.

Example Two: Removing Blank Lines

There are times when you need to remove all blank lines from a file. Here are two ways to achieve this from the command line:

:v/./d
:g/^$/d

You can apply either command to a file you are editing; they both remove any blank lines in a file. You also can control which lines are checked for blanks.

:10,20v/./d
:15,$g/^$/d

The first command checks for blanks in lines 10-20; the second command checks for blanks from line 15 to the end of the file.

There's an alternative to this method: Perform a manual search using the slash ( /) command line mode option, using the letter n to move to each occurrence of the pattern. When you find one to replace, use the change word ( cw) command. Press the Esc key and continue moving to each occurrence using the letter n. When you arrive at another one all you have to do is type the period ( .) key to execute the last command that replaced the pattern with a new word.

Look at the host table entries from before:

199.44.192.10   saturn.foo.com
199.44.192.11   jupiter.foo.com
199.44.192.12   uranus.foo.com

From the command mode

/foo

the cursor stops on the f of foo.

199.44.192.10   saturn.foo.com

Use the change word ( cw) to change foo to foobar and press the Esc key.

199.44.192.10.saturn.foobar.com

Press the letter n, which takes you to the next f in foo that it finds:

199.44.192.11   jupiter.foo.com

Leave this line alone and press the letter n again to move to the next f in foo it finds:

199.44.192.12   uranus.foo.com

Press the period ( .) and it automatically performs a change of the word foo to foobar because the cw command was entered last.

199.44.192.12   uranus.foobar.com

The final result is a change in the first and last domain names.

199.44.192.10   saturn.foobar.com
199.44.192.11   jupiter.foo.com
199.44.192.12   uranus.foobar.com

When you get the hang of it, you will find that in some cases, it can be faster than the nonvisual interactive searching-and-replacing. It can come down to personal preference in the end.

Reason

The search-and-replace operation is one of the most time-consuming tasks if you are forced to do it manually. There are some instances where you still have to. For the times where you don't, having the powerful ability that is supplied by the editor is a well-appreciated gift.

Real World Experience

There are so many places that this function can be applied on a day-to-day basis. If your company ever has to go through a massive domain name or subnet change, the modification to the hosts tables and DNS would be an enormously time-consuming task if not for the ability to perform a search-and-replace.

Have you ever had to migrate a large user base to a new fileserver where every home directory in the password file needed to be modified to point at the new fileserver? Being able to search and replace with tasks such as this shouldn't be taken for granted.

Other Resources

World Wide Web:

Geek-Girl--http://www.geek-girl.com/UNIXhelp/vi/ten.html

UNIX Hints & Hacks

ContentsIndex

Chapter 8: Editors

 

Previous ChapterNext Chapter

Sections in this Chapter:

   

8.1 The Anatomy of ed & vi

 

8.5 Abbreviating vi Commands

 

 

8.2 The Six Steps to ed

 

8.6 Creating Macros

 

8.10 Edit, Run, and Edit Again

 

8.3 Six Simple Steps to vi

 

8.7 Search and Replace

 

8.11 Reading STDOUT into vi

 

8.4 Configuring vi Parameters

 

8.8 Other Places to Use vi

 

8.12 Using vi when tmp Is Full

 

 

© Copyright Macmillan USA. All rights reserved.