sunglant:

If you had a complex sed script that you were converting to perl, then you may want to use s2p. But your sed script is simply doing a global search and replace. It also looks like shell quoting may be muddying the waters.

In general, if you see a sed invocation like sed -e 's#pattern#replacement#', then your perl code should look something like[1]:

# Standard setup stuff and file handle opening goes here while (<THE_FILE_HANDLE>) { s#pattern#replacement#; }

So a simple example could be:

#!/usr/bin/perl use strict; use warnings; # Get and open the input and output files my $INFname = shift; open my $INFH, '<', $INFName or die "Can't open '$INFName' for input: +$!"; my $OUTFName = shift or die "Missing filename(s)!\n"; open my $OUFH, '>', $OUTFName or die "Can't open '$OUTFName' for outpu +t: $!"; # Do the search and replace while (<$INFH>) { s/foo/bar/g; print $OUFH $_; }

So all you really need to do is pick apart your regular expression, make sure you can express it in perl without getting any quoting bits mixed in, and you should be good to go.

Notes:

[1] You can even do it on the command line like the sed example, but I don't generally use one-liners.

[2] The code is untested, and you'll probably want to modify it in any case.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Running complex sed from perl by roboticus
in thread Running complex sed from perl by sunglant

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.