I have a dump file with various INSERT statements.  The file looks something likestart_linesINSERT INTO table1234 INSERT INTO table1234 INSERT INTO table1234 INSERT INTO table7890 INSERT INTO table7890 INSERT INTO table7890  end_linesI need all the INSERT INTO table7890 statements to go above the INSERT INTO table1234 statements.  I would like to keep all the start_lines and end_lines in the same position they were before the switch occurs.  So, for instance, if the start_lines occupy lines 3 through 18, after switching the insert statements, they should continue to appear 3 through 18.  Ditto for the end lines.How do I switch the groups of statements ?
How do I swap two groups of lines in a .sql file?
text processing
The most simplistic approach is to read the file twice, 1st time you save the paragraphs and 2nd time your swap them e.g. with awk (in paragraph mode):awk -v RS= -v ORS='\n\n' 'NR==FNR{if (/table1234/) {one=$0} else if (/table7890/){two=$0}next}{if (/table1234/) {$0=two} else if (/table7890/){$0=one}}1' infile infileThis will print a trailing empty line, if it matters...Another way with ol' ed which does work in this particular case:ed -s infile <<\INg/INSERT INTO table1234/d-1r !grep -F 'INSERT INTO table7890' infile+1,$g/INSERT INTO table7890/d-1r !grep -F 'INSERT INTO table1234' infile,pqIN