[英]Finding all queries in a string in a source code using grep
我需要識別在不同目錄下的許多文件中傳播的php源代碼中的所有查詢。
我雖然使用grep和MySQL關鍵字來識別它們。 我可以通過兩種方式區分此源代碼中的查詢。
insert
, select
, update
, delete
和alter
。 但是有一個問題,雙引號中的查詢可以分布在多行中。 示例:
$newQuery = "Select username
from
usertable"
所以我需要確定"Select username from usertable"
但是grep無法在多行上運行。
我試過了 :
egrep -e '"(.*?)\+"' test.php | grep "select"
它適用於單行,但又錯過了多行查詢。
所以我試過了
sed -e '/\"/,/\"/!d' test.php
它返回所有查詢,但后來我做了
sed -e '/\"/,/\"/!d' test.php | grep select
它回來了,
"select
這不好。 我想我需要在sed中逃避換行。 我該如何實現這一目標? bash的任何其他標准命令也可以,例如awk。
我經常使用perl -pe
而不是sed
來獲得更多花哨的表達。
cat tmp.php | \
perl -pe "s/[\n\r\s]+/ /g" | \ # remove all spaces and line breaks
perl -e '$x=join " ", (<>); print join " ", ($x =~ /".*?(?:select|alter).*?"/gi)'
在最后一行中,您可以找到包含select
關鍵字的所有引號並加入它們
使用Perl的一種方法:
perl -00ne 'print $1,"\n" while (/"((select|insert|update|delete|alter).*?)"/sig);' file
要獲得單行輸出:
perl -00ne 'while (/"((select|insert|update|delete|alter).*?)"/sig){$x=$1;$x=~s/\n//g;$x=~s/\s+/ /g;print "$x\n";};' file
要使用join和split獲得單行輸出:
perl -00ne 'print join " ",split(/\s+/,$1),"\n" while (/"((select|insert|update|delete|alter).*?)"/sig);' file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.