[ILUG] joining columns/removing commas in a CSV file?

fergal daly fergal at esatclear.ie
Thu Nov 16 10:58:12 GMT 2000


kevin lyda wrote:
> 
> On Wed, Nov 15, 2000 at 06:55:56PM +0000, fergal daly wrote:
> > perl -pe '$i=1;s/,/$i++=~m#4|5|6#?"":","/ge' filename
> >
> > which is shorter and more perverse than anything I've seen so far, also,
> > you can get it to eliminate different commas just by changing the 4|5|6
> > stuff. Although if you have more than 9 commas it's probably best to
> > anchor the pattern with ^ and $
> 
> excellent.  want a job?  :)
> 
> kevin
> 
> ps  wrt the >9 commas, you mean '$i=1;s/,/$i++=~m#^4|5|6$#?"":","/ge', yes?
>     otherwise 14, 15, etc would match.

That's what I meant but unfortunately I was wrong! You need () too as
otherwise only the 4 is anchored to the start and the 6 to the end. It
should be

'$i=1;s/,/$i++=~m#^(4|5|6)$#?"":","/ge'

Although, if you have less than 26 coulmns you could do this

'$i="a";s/,/$i++=~m#d|e|f#?"":","/ge'


I sent this on to the guys at Fun With Perl and below is what they've
come up with so far. The shortest ones are specialised for the 4,5,6
problem and they all work pretty much the same way. Optimisitions
include replacing "," by $& and also rewriting the conditional as $& if
$i++=~m#^(4|5|6)$#

The last one is pretty nice, is the shortest non-specialised one so far
and does things completely differently


tayers at bridge.com wrote:
> 
> So far we have the following solutions.
> 
> From: fergal daly <fergal at esatclear.ie>
>   perl -pe'$i=1;s/,/$i++=~m#4|5|6#?"":","/ge'     43 chars
> 
> Yanick and Larry Rosler had several nice solutions, but they only work
> for files of 1 line since they don't reset their $i counter. Fixing
> that glitch gives us
> 
> From: yanick1 at sympatico.ca
>   perl -pe'$i=0;s/,/$&if++$i,!grep$i==$_,4,5,6/eg'  48 chars
>   perl -pe'$i=0;s/,/$&if 4>++$i||$i>6/eg'           39 chars
>   perl -pe'$i=0;s/,/$&if 1<abs++$i-5/eg'            38 chars
> 
> From: "Larry Rosler" <lr at hpl.hp.com>
>   perl -pe'$i=0;s/,/++$i!~m%^[456]$%&&$&/ge'        42 chars
>   perl -pe'$i=0;s/,/3<++$i&&$i<7?"":$&/eg'          40 chars
> 
> I shortened Yanick's best by one stroke as
>   perl -pe'$i=0;s/,/1<abs++$i-5&&$&/eg'             37 chars
> 
> Personally I took a different tack, but it's only good for a tie with
> 4th place.  I present it in hopes that it will spawn other
> solutions.
>   perl -F'(,)' -ane'@F[7,9,11]=();print at F'          40 chars
> 
> Hope you have a very nice day, :-)
> Tim Ayers (tayers at bridge.com)
> Norman, Oklahoma where I'm expecting Ilmari to improve my solution since
> I often follow his same strategy but overlook an obvious
> optimization. :-}
> 
> ==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
> ==== Send email to <fwp-request at technofile.org> with message _body_
> ====   unsubscribe




More information about the ILUG mailing list