A Developer's Diary

Dec 19, 2012

VI Editor - A great tool for testing Regular Expressions

VI is one of the fastest and most successful editor used by the unix users. VI can also boast of having a regular expression search feature which I found amongst easiest to use and easy to test your regular expressions as well

Consider the file pom.xml below. We are going to demonstrate a step by step way of selecting only line numbers in the file and finally performing a search and replace to remove all the leading line numbers from the text

1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4   <groupId>com.examples.spring</groupId>
 5   <artifactId>HelloSpring</artifactId>
 6   <packaging>jar</packaging>
 7   <version>1.0-SNAPSHOT</version>
 8   <name>HelloSpring</name>
 9   <url>http://maven.apache.org</url>
10   <dependencies>
11     <dependency>
12       <groupId>junit</groupId>
13       <artifactId>junit</artifactId>
14       <version>3.8.1</version>
15       <scope>test</scope>
16     </dependency>
17   </dependencies>
18 </project>

Type / to enter the search mode in VI

Step #1. Type ^. This will select all the characters at the beginning of the line

Step #2. Type ^ * OR ^\s* This will select all the characters at the beginning of the line followed by 0 or more space characters

Step #3. Type ^ *[0-9]\+ OR ^\s*\d\+ This will select 1 or more digits followed by the space character defined in step two

Step #4. Type ^ *[0-9]\+\s\{1\} OR ^\s*\d\+\s\{1\} This will select one more space character beyond the selection in step three if exists

The above expression selects only the line numbers. Once we are sure that our expression is working correctly, we can chose to execute find and remove command in VI editor itself. Go to command mode by pressing ESC and : and enter %s/^ *[0-9]\+\s\{1\}//gc OR %s/^\s*\d\+\s\{1\}//gc
You will be prompted everytime before a replacing a string. Once all the strings are replaced, you will have a file without the line numbers as shown below

No comments :

Post a Comment