I have a file with a lot of SQL INSERT constructs. I am trying to write a sed script to retrieve rows containing the name of an INSERT table.
INSERT INTO Table1 values( val1, vale2, val3 );
INSERT INTO Table2
VALUES( val1, vale2, val3 );
INSERT
INTO
Table3
VALUES( val1, vale2, val3 );
insert into table4
SELECT col1 from
table4
where condition1 = condition2
;
INSERT
INTO
table5 (col1, col2, col3)
VALUES( val1, vale2, val3 );
insert into table6 (col1,
col2,
col3, col4
)
SELECT col1, col2, col3,
col4 FROM
table6
WHEREcondition1 = condition2
;
My conclusion should be:
insert into table4
insert into table6
This is what I tried. However, I am not sure why my sed templates do not work. A script is pickup lines that have the word SELECT. I am using GNU sed 4.2. Any help would be greatly appreciated.
sed -n '/Insert/I,/;/ {
/^\s*$/{
i\ ...deleting blank line
=
d}
/into/I, /Select/I {
i\
...inside the Into---Select range
p
}
}' < testfile
My current output is above:
...inside the Into
INSERT INTO Table1 select values( val1, vale2, val3 );
...deleting blank line
120
...inside the Into
INSERT INTO Table2
...inside the Into
values( val1, vale2, val3 );
...inside the Into
INSERT
...inside the Into
INTO
...inside the Into
Table3
...inside the Into
values( val1, vale2, val3 );
...inside the Into
insert into table4
...inside the Into
SELECT col1 from
...inside the Into
INTO
...inside the Into
Table5 (col1, col2, col3)
...inside the Into
values( val1, vale2, val3 );
...inside the Into
insert into table6 (col1,
...inside the Into
col2,
...inside the Into
col3, col4
...inside the Into
)
...inside the Into
SELECT col1, col2, col3,