cibalo <
cib...@gmx.co.uk> writes:
> I have updated/modified my mysql database in my free website recently.
> I have to re-code all my php files such that an extra statement has to
> be inserted before the last mysql_query call. It may have more than
> one mysql_query call in some of my php files.
>
> I try to use a negative look-ahead assertion to make such a change as
> follows.
>
> I would like to insert just one "new_insert" line before the "coding
> line 4". And my php file looks like this.
>
> $ echo -e "coding line 1 test 1\ncoding line 2 mysql_query 2\ncoding
> line 3 test 3\ncoding line 4 mysql_query 4\ncoding line 5 test 5"
> coding line 1 test 1
> coding line 2 mysql_query 2
> coding line 3 test 3
> coding line 4 mysql_query 4
> coding line 5 test 5
Strictly, this isn't related to the shell and not related to Perl, but
if you want to make a modification to a set of text files, have you
considered using a text editor?
NB: The 'compound command' below was executed in a directory which
contained three copies of the 'demo file' whose content was included in
the original posting.
for x in *; do
ed "$x" <<'TT'
$
?mysql_query?i
new_insert
.
wq
TT
done
This invokes ed on every file in the current directory, with 'command
input' coming from a here document (separator quoted to prevent 'shell
expansion' of the text). The ed commands are
$
Go to the last line of the input file.
?mysql_query?i
Search backwards (regexp) for the first msql_query and insert text in
front of that line.
new_insert
The text to insert.
.
'Exit insert-mode' command.
wq
Write changes and quit (non-standard, BSD-originated command).