I need to identify all queries in a php source code spread in many files under different directories.
I though about using grep and MySQL keywords to identify them. There are two ways I can differentiate queries in this source code.
- They are always quoted in double quotes.
- They will always have the MySQL keywords such as
insert
,select
,update
,delete
andalter
.
But there is a problem, the queries in double quotes can be spread in multiple lines. Example :
$newQuery = "Select username
from
usertable"
So I need to identify "Select username from usertable"
But grep can not work on multiple lines.
I tried :
egrep -e '"(.*?)\+"' test.php | grep "select"
It works great for single line but again misses multiline queries.
So I tried
sed -e '/\"/,/\"/!d' test.php
It returns all the queries, But then I do
sed -e '/\"/,/\"/!d' test.php | grep select
It returns,
"select
which is not good. I think I need to escape newlines in sed. How do I achieve this? Any other standard commands for bash will also do, such as awk.