DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Grouping and Backreference in Replacement of vim


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:

  1. 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

  1. 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.



Published

Nov 1, 2013

Last Updated

Nov 1, 2013

Category

Tech

Tags

  • shell 46
  • Vim 92

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor