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

fergal daly fergal at esatclear.ie
Wed Nov 15 18:56:08 GMT 2000


Hope I'm not too late, busy day.

Here's a somewhat perverse way of doing things that only works because
all the fields to be joined are adjacent

perl -pi -e 's/((?:.*?,){3})(.*?),/$1$2/' filename filename filename

yes, you have to specify the filename 3 times!

Or maybe this

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 $

Fergal


kevin lyda wrote:
> 
> On Wed, Nov 15, 2000 at 11:32:34AM -0000, Gary McCloskey wrote:
> > big CSV file, need to merge particular columns.
> > => removing particular commas, e.g. to join the 4th,5th, 6th and 7th columns, remove the 4th, 5th, 6th commas
> > 1,2,3,4,5,6,7,8,9 => 1,2,3,4567,8,9
> >
> > thought that tr, cut or paste might help : just gave me a headache
> 
> does this make the headache go away:
> 
> perl -pe '/((?:.*?,){3})(.*?),(.*?),(.*?),(.*)/;$_="$1$2$3$4$5\n"'
> 
> i miss my computational theory course.  of course, what i really
> wanted was:
> 
> perl -pe '/((?:.*?,){3})(?:(.*?),){3}(.*)/;$_="$1$2$3$4$5\n"'
> 
> but the references don't work like that.  perhaps i'm missing something.
> 
> kevin
> 
> ps  no, in the past week of beating my head against modems, not a bit of
>     modem spew has actually been a valid perl program...
> 
> --
> kevin at suberic.net        q: what's the difference between batman & b. gates?
> fork()'ed on 37058400    a: when batman fought the penguin, he won.
> meatspace place: home                             http://suberic.net/~kevin/
>  >> want privacy? www.gnupg.org or www.pgp.com.  encrypted mail preferred <<
> 
> --
> Irish Linux Users' Group: ilug at linux.ie
> http://www.linux.ie/mailman/listinfo/ilug for (un)subscription information.
> List maintainer: listmaster at linux.ie




More information about the ILUG mailing list