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.
- Open the file sales in the vi editor (vi sales).
- 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.
- Enter :3 to move to line three of the file.
- 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
- 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.
- Enter /Burt to search for this pattern. After you locate the pattern, you perform any vi command on the entry.
- Type dd to delete the Burt entry.
- 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.