-1

I need to find all the table names alone in all select statements in my query log. Is there any way to grep (or) any other option, to take the strings that comes immediately after the word FROM in my file ..

Example: file contains something like this

select a, b, c FROM `table1` join `table2` etc...
insert into.................
commit
select * FROM `tablex` ..............
select y,s,h FROM `tabley`.................
.
.
. 

Now I want the list of the distinct tables alone from the select stmts. i.e,

table1
tablex
tabley
Inian
  • 80,270
  • 14
  • 142
  • 161
Preethi
  • 3
  • 3

1 Answers1

0

I'm not sure if I understand you correctly but if you want to extract the first word that comes after 'from' you can try something like this:

➜  tmp cat log 
select id, name from account where name = 'John';
select * from price where id > 0;
➜  tmp cat log | gawk -F'from ' '{print $2}' | gawk '{print $1}'
account
price
➜  tmp 

If you want to print names of joined tables as well you need to extend it.

g-t
  • 1,455
  • 12
  • 17
  • thank u @g-t :yes this is partly working fine. can i have this input in a file? since the count is high i dont get to c all the tables.. and yeah how to extend for the joined tables as well?? – Preethi Dec 09 '16 at 12:01