October 2012
346 pages
7h 40m
English
| Tip 93 | Rearrange CSV Fields Using Submatches |
In this tip, we’ll see how submatches captured from the search pattern can be referenced in the replacement field.
Let’s say that we have a CSV file containing a list of email addresses along with first and last names:
| substitution/subscribers.csv | |
| | last name,first name,email |
| | neil,drew,drew@vimcasts.org |
| | doe,john,john@example.com |
Now suppose that we want to swap the order of the fields so that the email comes first, then the first name, and finally the last name. We could use this substitute command to do it:
| => | /\v^([^,]*),([^,]*),([^,]*)$ |
| => | :%s//\3,\2,\1 |
In the pattern, [^,] matches anything that isn’t a comma. So ([^,]*) matches zero or more consecutive non-commas and captures the result ...
Read now
Unlock full access