Re: regex help with csv sql dump

From: Mike Bishop <mbishop@mtaonline.net>
Date: Sat Oct 21 2006 - 17:06:19 AKDT

On Sat, Oct 21, 2006 at 03:36:22PM -0800, Clay Scott wrote:
> i generated a mysql dump file for a database generated by an app i'm helping a friend write. now he's changed the database layout again and i'm having a hard time getting the dump file into the proper format to be imported back into mysql. in one particular table some fields have been added, but i've since learned how to use sed to add empty fields to the end of the csv string (thanks arthur!).
>
> my problem is that in one particular table the number of fields has been reduced. some fields have been done away with complete and the rest moved to another table. the field count has dropped from 30 to 24 and i'm having a hell of a time trying to get the regex right to drop the appropriate fields. basically the lines look like a standard csv dump file:
>
> INSERT INTO `table` VALUES ('value1', 'value2', 'value3', 'value4',....,'value30');
>
> it would be easier if the fields were all at the beginning or end of the string, but they're all throughout. i need to drop fields 9, 10, 11, 12, 14 and 20.
>
> any help would be greatly appreciated...
>

Clay,

awk is your friend. I'm a little rusty, but essentially you would just read the file
in after setting the field delimiter, then print out the fields you need, in the order you
need (adding the sql close after the last field).

Something like,

awk -F, 'BEGIN { OFS = "," }
{ print \
        $1,
        $5,
        $7,
        ...
        $n");" }' < filename

Here I am assuming $1 is required, so that "INSERT ....('value1'," is all viewed as
the first field. If that's not the case, strip out the "INSERT ..." stuff with sed,
and reinsert it before with the first field needed.

Let me know if you need any help,

Rgds,

--
Mike Bishop        907/495-5737                 mbishop@mtaonline.net
Willow, Alaska
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Sat Oct 21 17:06:51 2006

This archive was generated by hypermail 2.1.8 : Sat Oct 21 2006 - 17:06:51 AKDT