Splitting a complex file using awk

I have a file that contains the following format

delete from table1;
delete from table2;
insert into table1 (col1, col2) values (value1, value2)@
insert into table1 (col1, col2) values(value3, value4)@
insert into table2(col1, col2,col3) values(value1, value2, value3)@
etc
etc

This is in a single file, too big to edit.

The INSERT statements can be over multiple lines, and the terminator is always '@' sign.

There is a fixed number of tables and I know the name of these.

I have tried with awk using the following

awk '/^table1/{p=1}/^table2/{p=0;print}p' inputfile > outfile
awk '/^table2/{p=1}/^table3/{p=0;print}p' inputfile > outfile

This sort of works for anything apart from the first statement.

for the second statement it puts into the outfile

delete from table2
insert into table2(col1, col2,col3) values(value1, value2, value3)@
insert into table3........

But at least I can edit the file and remove table 3.

But there is probably a way of using awk to do this.

Thanks

Chris

Post desired output, that you want to get from this sample file.

file1

insert into table1 (col1, col2) values (value1, value2)@
insert into table1 (col1, col2) values(value3, value4)@

file2

insert into table2 (col1, col2) values (value1, value2)@
insert into table2 (col1, col2) values(value3, value4)@

file3

etc etc

So we have 1 file containing all the inserts for 1 table, 1 file for all the inserts for table 2 etc

Also I have just noticed that some of the INSERTS are over multiple lines.

eg.

INSERT INTO
table3 (col1, col2) values(val1 etc)

Try:

awk '/^insert/,/@/{if (/^insert/){n=gensub("^insert into ([^ (]+).*","\\1",1)};print > n}' big_file

It will create files with table names as filenames.

awk '/^insert/,/@$/{printf (NF>2)?$0 RS:$0 FS}' infile |awk '{print >$3}'

Thanks for the replies.
bartus11 .. when running yours I am getting an error Function gensub is not defined
I changed the insert to INSERT as in uppercase.

rdcwayx I am getting There are not enough parameters in printf statement when running yours.

here is a full insert statement that I have for one table (the others are similar but could be more or less columns)

INSERT INTO
polc.LITExpandata2   (OBJID,PARID,COMPANY,RECIND,NAME,SHORTNAME,RPTNAME,KEYVALUE,FLAG1,FLAG2,FLAG3,FLAG4,FLAG5,FLAG6,FLAG7,FLAG8,FLAG9,FLAG10,LANGUAGE) VALUES (-1,-1,4402,100000004,'0-2 YEARS OF EXPERIENCE                                     ','                    ','          ','B        ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','en')@
INSERT INTO
polc.LITExpandata2   (OBJID,PARID,COMPANY,RECIND,NAME,SHORTNAME,RPTNAME,KEYVALUE,FLAG1,FLAG2,FLAG3,FLAG4,FLAG5,FLAG6,FLAG7,FLAG8,FLAG9,FLAG10,LANGUAGE) VALUES (-1,-1,4402,100000004,'NOT APPLICABLE                                              ','                    ','          ','C        ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','en')@

Can you transfer that file to some Linux box and run my command there? :slight_smile:

bartus11.. I do have a local linux distro I could use, but not until later. should have mentioned I am running under AIX. I will transfer and let you know.

Thanks again

Seems your awk don't support (NF>2)?$0 RS:$0 FS

below code has been tested in AIX

awk '/^INSERT/,/@$/{if (NF>2) {printf $0 RS}else {printf $0 FS}}' infile |awk '{print >$3}'

Thanks to both for this. It works for the first couple of files, but looks like something is tripping it in the 3rd file. It does produce the file but stops.

I will investigate and see if I can spot anything.

clever piece of code.
awk: 0602-565 There are not enough parameters in printf statement polc.LITExpandata2 (OBJID,PARID,COMPANY,RECIND,NAME,SHORTNAME,RPTNAME,KEYVALUE,FLAG1,FLAG2,FLAG3,FLAG4,FLAG5,FLAG6,FLAG7,FLAG8,FLAG9,FLAG10,LANGUAGE) VALUES (-1,-1,1,55,'20% NCB ','20% NCB ','20% NCB ','20 ','N',' ',' ',' ',' ',' ',' ',' ',' ',' ','en')@
.
The input line number is 4948. The file is /home/db2inst1/StaticData/CustomerStaticDataExport-028.sql.
The source line number is 1.
awk: 0602-576 A print or getline function must have a file name.
The input line number is 3350.
The source line number is 1.