[Nottingham] Sed regex problem

notlug notlug at pendinas.org.uk
Thu Oct 13 07:48:49 UTC 2011


On 12/10/11 22:21, Dave Luff wrote:
> Hi List,
>
> Can anyone help me out on this one, which has got me tearing out the small remaining amount of hair even after googling.  I want 
> to match the minus sign character '-' in sed, but only if it occurs immediately after the 5th comma in a line that starts with 
> '0'.  So far I can match a minus sign following a comma in a line starting with '0', but matching it only after the 5th comma is 
> beyond me.  Any advice?
>
> (More generally, I'm trying to simply invert the polarity of one of the fields of a .csv file.  Is there an easier approach than 
> sed?)
>
Can you guarantee there are no text fields with quoted commas in them before the 5th comma ?
eg:  0,"A, really,silly, descriptive,-text-field-",x,x,x,-n, ...
or 0,"A not so silly description, but enough to mess with a simple script",x,x,x,-n,-m ..

I tend to use the perl Text::CSV module to parse CSV files for this reason.
Use perl regex's on the relevant field then follows naturally.

Have fun,
Duncan



More information about the Nottingham mailing list