You can group parts of the pattern expression enclosing them with "(" and ")" and refer to them inside the replacement pattern by their special number \1, \2 ... \9. Typical example is swapping first two words of the line:
:%s/(\w+)(\s+)(\w+)/\3\2\1/gc
Ref: Grouping and Backreferences of vim
A Demonstration
There are about 40 JUnit test classes in "java_test" folder of ESB project. Now we need add all class names and their respective package names into Excel Report.
Solution:
- Get original outputs with "find" command:
$ cd java_test $ find . -name '*.java' > report.csv
The original output of "find" command:
./com/boco/esb/dataquality/service/test/TestDataQualityRuleServiceImpl.java
The target output format:
TestDataQualityRuleServiceImpl,com.boco.esb.dataquality.service.test
- Convert format with vi:
$ vi report :%s/(.)\/(Test.).java/\2,\1/gc
Then open report.csv in Windows, it will be load by Excel automatically.