On Sep 2, 10:49 am, [EMAIL PROTECTED] (John W. Krahn) wrote:
> [EMAIL PROTECTED] wrote:
> > I'd like to apply a series of inline edits to a file in Perl.
>
> > With sed, I could use "sed -f commandfile inputfile" or in awk, "awk -
> > f commandfile inputfile", however, I could not find an equivalent in
> > Perl.
>
> perl commandfile inputfile
>
> > I'd prefer not to use sed or awk as they do not support inline
> > editing directly.
>
> The version of sed that I have supports -i[SUFFIX], --in-place[=SUFFIX].
>
> > In Perl, what would be the way to apply a series of multiple inline
> > edits in this form to an inputfile?
>
> > commandfile:
> > s/searchterm/replaceterm/
> > s/searchterm2/replaceterm2/
> > s/searchterm3/replaceterm3/
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> $^I = '';
> while ( <> ) {
>      s/searchterm/replaceterm/;
>      s/searchterm2/replaceterm2/;
>      s/searchterm3/replaceterm3/;
>      print;
>      }
>
> __END__
>
> Then make the file executable:
>
> chmod 0750 /path/to/commandfile
>
> And then run it like this:
>
> /path/to/commandfile inputfile
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order.                            -- Larry Wall

Because I am using Gnu-sed and do have the in-place switch, I wound up
going with a sed solution.

So this worked:

sed -i '' -f commandfile inputfile

The only tricky bit here is that you are forced to use a "0-length
extension" with the -i switch, which in this case is two single quotes
together with no spaces and NOT a double quote.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to