tags:

views:

167

answers:

2

I have a 500,000 line sql script.

update users set region_id = 9814746 where id = 101 and region_id is null;
update users set region_id = 9814731 where id = 102 and region_id is null;
update users set region_id = 3470676 where id = 103 and region_id is null;
...
....

I need to be able to generate a script with begin...commit every 50 lines. How do I do this? I'll be running this in pg-admin.

Thanks, Sharadov

+5  A: 

This might be a good start:
cat sql.file | perl -e 'i=0;print "begin;\n"; while(<>){print;i++;if(i % 50 == 0){print "end;\nbegin;\n";}}print "end\n";' > outputsql.file

chotchki
I have no perl skills whatsoever.
sharadov
You don't need perl skills. Just use the code that chotchki provided.
depesz
+2  A: 

Simple awk solution:

cat your_sql_file | awk '
    BEGIN{print "BEGIN;"}
    {print}
    (0 == NR%50) {print "COMMIT;\nBEGIN;"}
    END{print "COMMIT;"}'
depesz
Worked like a charm! Now understand the power of the shell.
sharadov
worked like a charm! how understand the power of the shell.
sharadov
for PROGRAM in sed awk grep cut cat tac nl; do man $PROGRAM; done; while true; do play_with_it; done
depesz