I think your problem stems from the backslashes in your replace pattern. It's really hard (at least for me) to figure out how many times to escape in double quotes which will then be interpreted by the shell (do I need two backslashes here or four? More?). I have a feeling you might have needed 4 backslashes there, but there's a better way to do what you want...

You call sed like this:

system("sed -e 's/$search/$replace/g' $File1 > $Replace_html");
This is generally considered a bad and very unsafe way, not to mention at times unpredictable. What if $search, $replace, or any other variable has the value ";; rm -rf / ;;" ? What will the command line be equivalent to? You should call external programs using a method that does not use the shell, or automatically espaces arguments. May I suggest:
open (my $fh, "-|", 'sed', '-e', "s/$search/$replace/g", $File1); my $new_file = do { local $/; <$fh> }; # grab sed's output # now you can write the contents of $new_file to a file yourself.
This open call executes sed with the arguments you want, and sends its output back do the filehandle $fh. You don't have to worry about the shell messing up the characters in the arguments, because this mechanism bypasses the shell, making it very safe.

Another alternative would be to use IPC::Open2 or IPC::Open3, which will let you execute a command in the same safe way as above, and send its output to the file all in one statement. But I'll leave that as an assignment for you -- it's a very similar mechanism, however.

Also, the next time you submit a question (or an answer?) that contains code, use <code> tags, and check the previews of your post to make sure they look readable! Hey, is there an echo in here? ;)

blokhead


In reply to Re: need help on SED by blokhead
in thread need help on SED by Anonymous Monk

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.