edit:
I need advice on best way to search with regex in vim and extract any matches that are discovered.
I have a csv file that looks something like this:
Two fields:
-
id
-
description
0g98932,"long description sometimes containing numbers like
1234567, or 0000012345 and even BR00012345 but always containing text"
I need to search the description field on each row.
If a number matching \d{10} exists in the second field, I want to pull it out.
doing something like :% s/(\d{10})/^$1/g gives me a
Pattern not found (\d{10}) error.
I’ve never learned how to grab and reference a match from a regex search in vim – so that’s part of the problem.
The other part:
I would really like to either.
- Delete everything other than the first 7 digit id and the matches.
- Copy the id and the matches to another file – or to the top of the current file (somewhere – anywhere just to separate the matches from the unfiltered data).
The important thing to know about vim regexes is that different levels
are escaping are required (as opposed to, say, regexes in Perl or Ruby)
From
:help /\mThe default setting is ‘magic’, so to make the regex you gave worked, you’d
have to use:
If you want to delete everything other than the first 7 digit id and the matches
(by which I assume you mean that you want to delete lines without any match)
The
:v/<pattern>/command allows you to run a command on each line that doesn’t matchthe given pattern, so this just deletes the non-matches.
:s//reuses the prior pattern,so we don’t have to specify it.
This transforms the following:
into this: