0

I have php files with SQL queries broken into multiples lines. for example:

$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;

I'm trying to extract all SQL statements from the PHP files. I tried grep on $sql and only getting the first line back.

How do I extract entire SQL string from all PHP files?

I was thinking more around deleted line break until ';' character.

1
  • Seeing as the SQL contains PHP constants, what use it this going to be? You'd be better off to parse the string in PHP and output it no? Commented Sep 23, 2015 at 0:47

1 Answer 1

1

You can try this sed,

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

Test:

$ 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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.