Vi editor: search, replace commands and exercises. Regex.

Created on 2011-03-24 by Sona Gabrielyan

Switzernet

 

 

Introduction. 1

Requirements. 1

Key rules. 1

Commands. 2

Search. 2

Replace. 2

Regular Expressions. 3

Training session. 3

Validation. 7

References. 7

 

 

Introduction

In this document we discuss some examples of usage functions of vi for search and replace text. Additionally, here we give some basic information and examples for regular expressions (RegEx).

 

 

Requirements

Make sure you followed the training session on vi: http://switzernet.com/3/public/110211-training-vi/

 

 

Key rules

vi editor that it has two modes: command and insert. The command mode allows the entry of commands to manipulate text. The insert mode puts anything typed on the keyboard into the current file.

 

vi starts out in command mode.

 

There are several commands that put the vi editor into insert mode. The most commonly used commands to get into insert mode are a and i.

 

Once you are in insert mode, you get out of it by hitting the escape (Esc) key. Hitting Esc while you are already in command mode doesn't take the editor out of command mode. It may beep to tell you that you are already in that mode.

 

The following rule is to be kept always in mind while working with vi: when finish typing a text in vi, one must press Escape key.

 

Some most commonly used commands are described here: http://switzernet.com/3/public/110211-training-vi/

 

Remember: if you need to undo previous changes, type u in command mode. If you push u once again, you will come back to result. So u switch you between last change and previous stage.

 

Commands

Search. 2

Replace. 2

Regular Expressions. 3

 

Search

/string - search forward for string

?string Search back for string

n - Search for next instance of string

N - Search for previous instance of string

 

Example:

 

 

Search string test and stop when find one. To continue search for next instance, push n.

 

 

 

Replace

For search and replace we use “ :s “command (s comes from “substitute").

 

The format of the command is the following:

 

:s/pattern/string/flags - replace pattern with string according to flags. Here pattern is what we are searching for. Usually, it is specified by a set of regular expressions.

 

g - flag - Replace all occurrences of pattern (if this flag is not used, vi will substitute only  for the next instance of the pattern)

c - flag - Confirm replaces

i - flag - Ignore the case (takes into account all occurrences of the string in upper case or lower case)

 

 

A prefix can be used before s command to indicate where to search.

 

1,n - search from the first string (1) until the string number n

.,n - search from current string until the string number n

1,$ - search from the first string (1) until the last string ($)

 

Examples of replace command:

 

Here we search in the text located between strings N°1 to N°5 all instances of pattern « ooo » and replace it with « zzz ».

 

 

If we have OOO or oOO or Ooo in all the text from the first to the last lines, and would like to replace all of them by zzz, we add i flag:

 

 

 

 

Regular Expressions

[regex]

A regular expression, commonly known as "RegEx", is a set of characters that specify a pattern. Regular expressions may be used within searches, so we have a control over what we are searching for.

 

The very basic regular expressions are:

. (dot) - any single character except new-line character

* - zero or more occurrences of any character

^ - anchor - beginning of the line

$ - anchor - end of line

\d - a digit (0 to 9)

\n - a new line character

[...]- any single character specified in the set (see examples of set [] set below)

[^...]- any single character not specified in the set

 

Examples of set:

 

[A-Z] - The set from Upper-case A to Upper-case Z

[a-z] - The set from Lower-case a to Lower-case z

[0-9] - The set from 0 to 9 (all numerals)

[./=+]- The set containing . (dot), / (slash), =, and +

[-A-F] -The set from Upper-case A to Upper-case F and the dash (dashes must be specified first)

[0-9 A-Z] -           The set containing all capital letters and digits and a space

[A-Z][a-zA-Z] - In the first position, the set from Upper-case A to Upper-case Z, in the second character position, the set containing all letters (upper- and lower- case)

 

 

 

Training session

Run the Cygwin.

Type “date” command to show the current date and time.

Create a folder where you will keep your files during the current training session.

Change current directory to that folder. For example, if the folder is in C:\data\folders\110324-training-vi, then type in Cygwin the following command:

 

cd "C:\data\folders\110324-training-vi"

 

Open a MS Word document, name it in format yymmdd-lname-vi-regex.doc, where yymmdd is the current date and lname is your lastname.

 

1) Make a prinscreen of Cygwin (Alt+PrintScreen) showing the two commands that you just typed and past the picture into the MS Word document. Save. See the example below:

 

 

 

2) Download the following file into the created folder: [txt].

 

Open the downloaded txt file with vi. Try the search command (e.g. /begin will search for begin).

 

To go to the last line, type the following command:

 

:$

 

Now go to the first line:

 

:1

 

Go to the section “Inserting” of the file. To quickly find the section, type the following command:

 

/Inserting

 

You will see, your cursor will be placed under the first appearance of the word “Inserting”. Attention, the search command (/) is case-sensitive. If you search for /inserting, you will get zero result.

Add the current date, your first and last name on the next line after “Inserting”.

 

Add “OOOOO” in 3 different places in the section “Inserting” of the text. Make a printscreen:

 

 

 

3) Replace all “OOOOO” by “zzzzz” using substitute command (:s) discussed during the training session.

Make a prinscreen and copy it into your rapport file.

 

 

Exit without saving:

 

:q!

 

 

4) Type vi and yymmdd-lname-vi.txt to begin with editing session. Here yymmdd is the current date, lname is your lastname.

 

For example:

 

$ vi 110324-gabrielyans-vi.txt

 

 

Make a prinscreen of Cygwin (Alt+PrintScreen) and past it into the MS Word document. Save.

 

5) To begin inserting text, press i. Type the current date, your name. Press ESC to switch from the insert to the command mode. To save and exit, type ZZ - save and exit (hold down shift and press "z" twice).

 

Open again the same file.

 

$ vi 110324-gabrielyans-vi.txt

 

 

Make a prinscreen of Cygwin (Alt+PrintScreen) and past it into the MS Word document. Save.

 

6) Copy any 3 lines from this text into the .txt file which you have just created. For example:

 

 

 

Make a prinscreen of Cygwin (Alt+PrintScreen) and past it into the MS Word document. Save.

 

7) chose a character (for example, m) and replace all instances of that character by it’s uppercase (if you chose m, replace it by M in the whole text).

Write the command that you used in the MS Word document. Make a prinscreen of your txt file showing the text after changes (see example below) and past it into the MS Word document:

 

8) Using a command, add at the beginning of each line the following text: yymmdd-fname-vi-training--- where yymmdd is the current date, fname is your first name. Write the same command in the MS Word document, make a prinscreen of your text after change and copy it in the MS Word document. See example below:

 

 

 

9) Replace all digits by the first letter of your first name. Write the command which you used in MS Word document and provide the prinscreen of result as in example below:

 

Save your txt file and exit:

 

:ZZ

 

 

Your MS Word document must contain now 9 printscreens and 3 commands. Save. Exit.

 

 

 

Validation

Upload the rapport on the training session web site, according to the guidelines.

 

 

References

This document: [ch1] [ch2]

Vi Cheat Sheet: http://www.lagmonster.org/docs/vi.html

Vi Cheat Sheet: [cached]

Search and replace with vi: http://www.kingcomputerservices.com/unix_101/search_and_replace_with_vi_part_1.htm

Vi editor basic commands: http://switzernet.com/3/public/110211-training-vi/

Training sessions: http://www.unappel.ch/2/support/100722-training-employees/i/

 

*   *   *