I need to extract everything between the SQL keywords FROM and WHERE from a file. There can be multiple combination of FROM and WHERE. Also this has to be case insensitive match. The SQL file is as below:
SELECT col1 as column1,
Col2 as column2,
Col3 as column3,
(SELECT t1.col4
from table_1 t1, table_3 t3
WHERE t1.col5 = t2.col6
AND t1.col2 = t3.col11
) as column4
FROM
table_2 t2,
table_4 t4
where t2.col7 ='Active'
AND t2.col12 = t4.col13
AND t2.col8 IN ('abc','def','ghi')
AND t2.col8||''||t2.col9 <> 'jkl'
AND t2.col10 IS NULL;
Output desired should be
table_1 t1
table_3 t3
table_2 t2
table_4 t4
I have tried the following which almost solves the problem, except that if the table name is present in the next line where "FROM" is, this breaks and doesn't prints as output.
#!/bin/sh
awk '
BEGIN {IGNORECASE=1} { found = 0; }
/FROM/ {
if (!found) {
found = 1;
$0 = substr($0, index($0, "FROM") + 4);
}
}
/WHERE/ {
if (found) {
found = 2;
$0 = substr($0, 0, index($0, "WHERE") - 1);
}
}
{ if (found) {
print;
if (found == 2)
found = 0;
}
}
'
The only problem with the that solution is it doesn't work if the FROM keyword and the table names are in different line like in the example sql above.
– Mohammed Arif Jun 03 '20 at 10:45