I’m trying to parse through some massive SQL query logs using sed and want to extract the names of all the tables that are joined in each query. Here’s what I’m trying:
echo '[SQL_STMT="SELECT A FROM TABLEA JOIN TABLEB ON SOMETHING JOIN TABLEC ON SOMETHINGELSE WHERE A = 1"]' \
| sed -e 's/SQL_STMT=.*JOIN \([A-Z0-9_]*\) .*\]$/SQL_JOINS="\1"]/g'
but this only returns the following:
[SQL_JOINS="TABLEC"]
what I’d like to see is something like this:
[SQL_JOINS="TABLEB TABLEC"]
and have it work for any number of joins
So how do I structure the backreference so that it gets all the joined tables?
If you are ok with perl solution, try this:
On testing with above:
Sed solution:
The first sed prints all the table names, the second joins all lines of table names into one, and the last one formats it to get in the required format.