I\'m trying to replace text in a multi-line file using command-line perl. I\'m using Ubuntu Natty.
Below is the content of my text file (called test.txt):
You are reading the file line-by-line, so only the first line matches your regex. What you'll want to do -- if you truly wish to delete most of the content -- is to slurp the file by using the -0
option, e.g. -0777
. This is line ending processing, and 777
is just a number used by convention as an octal number large enough so as to cause file slurping.
perl -0777 -i -pe 's/(\[mysqld\][^\^]+)/$1\nsometext/g' test.txt
Also, I replaced your quotes. If you are in *nix, which it seems you are, single quotes are preferable. Case in point, $1
would not be interpolated by the shell.
The -p switch causes Perl to iterate over every line of the input and execute the given code for each of them (and to print the lines afterwards). Specifically, the command
perl -p -e 'SOME_CODE_HERE;'
is exactly equivalent to running the following Perl program:
LINE: while (<>) {
SOME_CODE_HERE;
} continue {
print or die "-p destination: $!\n";
}
Your regexp seems to be intended to match multiple lines at once, which obviously won't work if Perl is processing the input line by line. To make it work as intended, you have (at least) two options:
Change Perl's notion of what constitutes a line by using the -0NNN switch. In particular, the switch -0777
causes Perl to treat every input file as a single "line".
Rewrite your code to e.g. use the .. flip-flop operator.
By the way, I strongly suspect that your regexp doesn't mean what you think it means. In particular, [^\^]+
matches a string of one or more characters that doesn't contain a caret (^
). Since your input doesn't seem likely to contain any carets, this seems essentially equivalent to (?s:.+)
(or just .+
if you use the /s modifier).