0

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

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
sharadov
  • 968
  • 2
  • 13
  • 32

2 Answers2

5

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
  • 4,258
  • 5
  • 34
  • 55
3

Simple awk solution:

cat your_sql_file | awk '
    BEGIN{print "BEGIN;"}
    {print}
    (0 == NR%50) {print "COMMIT;\nBEGIN;"}
    END{print "COMMIT;"}'