[Nottingham] Sed regex problem

Dave Luff daveluff at ntlworld.com
Thu Oct 13 21:14:49 UTC 2011


Thanks for all the suggestions everyone!  My favourite was Camilo's awk 
suggestion, I ended up with:

awk -F, '{OFS=","; if ($1 == 0) $6 = -$6; print}' filename.csv

which worked great - there is no pathological quoting of commas in my 
input fortunately.

Michael Erskine wrote:
> 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)
>   

In principle I agree with this.  My problem is that for the last x years 
I have been reaching for my favourite language in order to sort little 
data munging problems like this, the problem being that my favourite 
language is C / C++, which really is completely unsuited to this kind of 
small messy task.  So I've been trying to use sed and awk wherever 
possible recently, in the aim that if I use them enough I'll eventually 
remember enough of them to use them without constant recourse to google 
even after a break.  One day I'll even get around to looking at Perl (I 
have tried in the past, but sufficiently infrequently that I have to 
re-learn everything each time, which is a pain!).

Cheers - Dave





More information about the Nottingham mailing list