I need to update a website that is moving to a new host. I would like to add the following line to each file, only once, after the first occurrence of <?php:
define('FRAMEWORK_LOCATION', '/home/someUser/framework.php');
I have tried several variations of this Perl oneliner:
$ find . -name '*' -print0 | xargs -0 perl -pi -e 's|<?php|<?php\rdefine('\''FRAMEWORK_LOCATION'\'', '/home/someUser/framework.php');'
However, as can be seen it would affect all lines. I am not particularly concerned about the case of additional code after the initial <?php though if a solution does take that into account then that would be good for me to learn from as well.
You can directly use sed, without invoking perl:
The relevant bit is:
where you are specifying:
0,/<?php: from the first line to the first occurrence of<?phps||&\nNEW_TEXT|: where you substitute the previous match<?phpwith itself followed by a new line with some new text.Note that I added the switch
-type ftofindfor filtering out directories.