regex - Script to add line with part of the pattern used to find the line -


i'd parse *.php files, , each line

$res = $db -> query($queryvar); 

i need get:

file_put_contents('php://stderr', print_r($queryvar, true)); $res = $db -> query($queryvar); 

the name of variable $queryvar may change! need code!

my initial idea:

find  -not -path "*/\." -name "*.php" -type f -print0 | xargs -0 sed -i 's,something,something,' 

but seems not possible name of query variable sed.

i started looking @ perl: perl: append line after last line match pattern (but incrementing part of pattern)

but able this:

perl -pe 's/(-> query\(.*\))/aaaaa $1 aaaaa\n$1/'  < filename.php 

with 2 problems: result on standard output, need sed edit original file, call find | xargs , anyway whole found line , not variable:

    $res = $db aaaaa -> query( $sql) aaaaa -> query( $sql); 

given file named filename.php, can run following command:

perl -pi -e 's/^(.+-> query\((.+?)\).*)$/file_put_contents\("php:\/\/stderr", print_r\($2, true\)\);\n$1/;' filename.php 

it update file in-place substitution intended perform.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -