[PLUG] Awkward Field Separators

Robert Citek robert.citek at gmail.com
Tue May 13 15:55:24 UTC 2008


On Tue, May 13, 2008 at 10:04 AM, Rich Shepard <rshepard at appl-ecosys.com> wrote:
>    I'm trying to write an awk/gawk script to alter the field sequence in a
>  data file containing about 300 lines. Because of different field separators
>  in each line I've not figure out how to write an expression that works.
>
>    The file is to insert data into a SQL database table. The original lines
>  are like this one:
>
>  INSERT INTO "Rules" VALUES('Few','Inorganics','Aquifer','GroundWater'
>
>    What I need for output is this:
>
>  INSERT INTO "Rules" VALUES('GroundWater','Aquifer','Inorganics','Few'
>
>  so I can finish the statements.

Can you use sqlite?

$ apt-get install sqlite

$ sqlite3 foo.db "create table Rules (a,b,c,d);"
$ cat <<eof | sqlite3 foo.db
INSERT INTO "Rules" VALUES('Few','Inorganics','Aquifer','GroundWater');
eof
$ sqlite3 foo.db "alter table Rules rename to bar;"
$ sqlite3 foo.db "create table Rules as select d,c,b,a from bar;"
$ sqlite3 foo.db ".dump Rules"
BEGIN TRANSACTION;
CREATE TABLE 'Rules'(d,c,b,a);
INSERT INTO "Rules" VALUES('GroundWater','Aquifer','Inorganics','Few');
COMMIT;

Pipe that through grep to get just the inserts.

Regards,
- Robert



More information about the PLUG mailing list