2015-09-23 96 views
0

我有SQL文件與SQL查詢分成多個行。 例如:unix命令刪除換行符爲php命令

$sql = "select count(aa." . BOOK_ART_ID . ") as book_count 
       from " . BOOK_ART_TABLE . " as aa 
       inner join " . AUTHER_TABLE . " as l on aa." . BOOK_ART_AUTHER_ID . " = l." . AUTHER_ID . " AND 
        l." . AUTHER_CODE . " = '" . "'  
       where aa." . BOOK_ART_TITLE_ID . " = " . $book_id; 

我試圖提取從PHP文件中的所有SQL語句。我在$ sql上嘗試了grep,並且只返回第一行。

如何從所有PHP文件中提取完整的SQL字符串?

我正在考慮更多地圍繞刪除換行符,直到';'字符。

+0

看到作爲SQL包含PHP常量,有什麼用這將是?你最好在PHP中解析字符串並輸出它不是? –

回答

1

你可以試試這個sed

sed -n '/\$sql/{ :loop; N; s/ *\n *//g; /;/{p;q}; t loop}' yourfile 

測試:

$ sed -n '/\$sql/{ :loop; N; s/ *\n *//g; /;/{p;q}; t loop}' yourfile 
$sql = "select count(aa." . BOOK_ART_ID . ") as book_countfrom " . BOOK_ART_TABLE . " as aainner join " . AUTHER_TABLE . " as l on aa." . BOOK_ART_AUTHER_ID . " = l." . AUTHER_ID . " ANDl." . AUTHER_CODE . " = '" . "'where aa." . BOOK_ART_TITLE_ID . " = " . $book_id;