Searching for a Pattern – Using vi Editor


Searching for a Pattern

You might need to find specific entries in a file .This can be time-consuming and potentially inaccurate, especially in large files. The /pattern command enables you to find a specific pattern or entry in files. It finds the first occurrence of the pattern following your current cursor position. To search for the next occurrence of the pattern, type the letter n. To continue to search for additional occurrences. Continue to type n.

Searching within a file

 Objective:  To set line numbers, search for a pattern, and repeat the last command.

  1.   Open the file sales in the vi editor   (vi sales).
  2.   Enter :set number to turn on line numbering. This is useful in large files so you can get an idea of where you are in the file.
  3.   Enter :3 to move to line three of the file.
  4.   Type / to enter the search mode, and then enter Talbot to specify the search pattern. Notice that the cursor jumps to the first entry after the cursor containing the pattern Talbot
  5. Type n several times to take you to the rest of the Talbot entries. Notice that it wraps around to the top of the document.
  6. Enter  /Burt to search for this pattern. After you locate the pattern, you perform any vi command on the entry.
  7. Type dd to delete the Burt entry.
  8. Type n to move to the next Burt entry, and then type. (a single period) to repeat the delete command.

Repeat this step until all Burt entries have been deleted.



About Author


Leave A Reply

Powered by