I have a file with one insert on each line, like below:
INSERT INTO table (columns) values (1,x,b);
INSERT INTO table (columns) values (2,y,c);
INSERT INTO table (columns) values (3,w,d);
INSERT INTO table (columns) values (4,z,e);
-- Comment
SELECT * FROM table;
-- Comment
SELECT * FROM table;
How do I remove every line after the last ocurrence of INSERT INTO? Output I'm looking for is:
INSERT INTO table (columns) values (1,x,b);
INSERT INTO table (columns) values (2,y,c);
INSERT INTO table (columns) values (3,w,d);
INSERT INTO table (columns) values (4,z,e);
The easier way is to use tac
:
tac file|sed '0,/^INSERT INTO/{/INSERT INTO/!d}'|tac
You can also use awk without tac
:
awk 'NR==FNR{if(/^INSERT INTO/)e=NR;next}FNR<=e' file file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With