The alternative is the suggestion by the other poster - run through each line and use a flag to tell you whether you are in a replaceable section.
The more awkward problem is how to replace the text in the file. Again, it is perfectly valid to just open an INPUT filehandle and an OUTPUT filehandle, then to rename the new file to overwrite the old one. However, that is quite complex. A neater idea might be to write a shell script and use some of perl's command line switches. (See perlrun for details.) Something like:
That's untested but you get the general idea. Find finds and prints the relevant files. xargs takes the filename and passes it to the end of the perl command. perl reads the filename, prints a copy to filename.bk (you can remove the .bk once you are sure your pattern works), and alters the original file.find . -name *.ext -print | xargs perl -pi.bk -e \ ' if ($flag && /end_match/) {$flag = 0;} if ($flag) { $sub = $printed++? '': 'My text to substitute'; s/^.*$/$sub/; } if (/start_match/) {$flag++;$printed = 0;} '
No good if you're on windows though...
dave hj~
In reply to Re: Modifying A Bunch of Files by replacing specific lines
by dash2
in thread Modifying A Bunch of Files by replacing specific lines
by ishaqali
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |