This type of search and replace can be done with a single layer, such as
perl -i -pe 's/START.*STOP/replace_string/g' file_to_change
For more ways to do the same thing:. To process multi-line searches, use the following command -
perl -i -pe 'BEGIN{undef $/;} s/START.*STOP/replace_string/smg' file_to_change
To convert the following code from a single line to a perl program, see the perlrun documentation.
If you really need to convert this to a working program, just let Perl handle the file open / close for you.
#!/usr/bin/perl -pi #multi-line in place substitute - subs.pl use strict; use warnings; BEGIN {undef $/;} s/START.*STOP/replace_string/smg;
Then you can call the script with the file name as the first argument
$perl subs.pl file_to_change
If you need a more meaty script where you can handle file open / close operations (do we like all these βmentalβ statements), look at the example in perlrun under the -i [extension] switch.
aks Jun 23 '09 at 5:57 2009-06-23 05:57
source share