Start ... complete every 50 lines

I have 500,000 lines of 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 can I do it? I will run this in pg-admin.

Thanks, Sharadov

0
postgresql
source share
2 answers

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

+4
source share

A simple awk solution:

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

All Articles