This assumes that your sections start and end on seperate lines...if that's not the case an slightly different version is required. See perlman:perlrun for the details of the switches.

On a *nix system you don't need the first line of the BEGIN{} block

#! perl -snli.org use strict; use vars qw[$B $E $R $s]; BEGIN{ @ARGV= map{glob}@ARGV; # Not necc. on *nix $s=0 } /<!- $B -->/ .. (/<!- $E -->/ && ($s = 1)) ? ($s and print($R), $s = 0 ) : print

sample usage

C:\test>del junk.in && ren junk.in.org junk.in C:\test>type junk.in -i[*extension*] If the extension doesn't contain a "*", then it is appended to th +e end of the current filename as a suffix. If the extension does contain one or more "*" characters, then each "*" is replaced wit +h the current filename. In Perl terms, you could think of this as: ($backup = $extension) =~ s/\*/$file_name/g; This allows you to add a prefix to the backup file, instead of (o +r in addition to) a suffix: <!- BRS --> $ perl -pi 'orig_*' -e 's/bar/baz/' fileA # backup to 'orig +_fileA' <!- ERS --> Or even to place backup copies of the original files into another directory (provided the directory already exists): $ perl -pi 'old/*.orig' -e 's/bar/baz/' fileA # backup to 'ol +d/fileA.orig' <!- BRS --> These sets of one-liners are equivalent: <!- ERS --> $ perl -pi -e 's/bar/baz/' fileA # overwrite curre +nt file $ perl -pi '*' -e 's/bar/baz/' fileA # overwrite curre +nt file $ perl -pi '.orig' -e 's/bar/baz/' fileA # backup to 'file +A.orig' $ perl -pi '*.orig' -e 's/bar/baz/' fileA # backup to 'file +A.orig' C:\test>test -B=BRS -E=ERS -R="Replacement text" junk.in C:\test>type junk.in -i[*extension*] If the extension doesn't contain a "*", then it is appended to th +e end of the current filename as a suffix. If the extension does contain one or more "*" characters, then each "*" is replaced wit +h the current filename. In Perl terms, you could think of this as: ($backup = $extension) =~ s/\*/$file_name/g; This allows you to add a prefix to the backup file, instead of (o +r in addition to) a suffix: Replacement text Or even to place backup copies of the original files into another directory (provided the directory already exists): $ perl -pi 'old/*.orig' -e 's/bar/baz/' fileA # backup to 'ol +d/fileA.orig' Replacement text $ perl -pi -e 's/bar/baz/' fileA # overwrite curre +nt file $ perl -pi '*' -e 's/bar/baz/' fileA # overwrite curre +nt file $ perl -pi '.orig' -e 's/bar/baz/' fileA # backup to 'file +A.orig' $ perl -pi '*.orig' -e 's/bar/baz/' fileA # backup to 'file +A.orig'

Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.


In reply to Re: Modifying A Bunch of Files by replacing specific lines by BrowserUk
in thread Modifying A Bunch of Files by replacing specific lines by ishaqali

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.