[Nottingham] Sed regex problem

Michael Erskine msemtd at googlemail.com
Thu Oct 13 08:10:31 UTC 2011


As Duncan says, if it's hard to "express" in a single "expression"
then don't bother: write a little script in your favourite language.
And it you don't have a favourite, just pick one! I tend to reach for
Perl and so I'd find a module that does CSV properly (and Text::CSV
looks like the right thing, as does Text::ParseWords)

If you really want to approach it as a regex-challenge then I'd start
with matching a sequence of non-comma characters followed by a comma
would be...

([^,]+),

...then a group of five would be something like...

(([^,]+),){5}

...but as soon as there's anything I can't immediately read and
understand (i.e. requires comments) then a script is probably a better
way...

use Text::ParseWords;
@fields = parse_csv($line);

...then do whatever with $fields[4]

Having said that, if I was in vim with the file open I'd be able to
try out my regex interactively so I could play until I got something
that worked. Lots of Fun.

Regards,
Michael Erskine.



More information about the Nottingham mailing list