Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone , below is a snippet of my code, I am using sed to do some modifications in a file but for some reason the output file is of 0 length, but when i run sed through command line it does the right changes, below is the code
foreach my $row (@rows) { foreach my $key (keys %$row) { $tempc =""; $tempb =""; $tempa = $row->{$key}; $tempc = "href=\"$paths\""; if ( defined $tempa ) { if ($tempa =~ /$domain/) { $tempb = $1 if $tempa=~ /$domain(.*)/; } else { $tempb = $tempa; } $tempb = "href=\"$tempb\""; #print "your old $tempc is being replaced by $tempb <br>\n" +; (my $search = $tempc) =~ s[/](\\/)g; (my $replace = $tempb) =~ s[/](\\/)g; print "search = $search and replace = $replace"; system("sed -e 's/$search/$replace/g' $File1 > $Replace_html +"); } else { push ( @NotFound, $tempc ); } } } } #end of else } #end of big foreach and $search = 'href="\/spanishresources.jsp" ' and $replace = 'href="\/spanish.html"'
so and so fourth
please help
thank you

20030527 Edit by Corion: Added formatting

Replies are listed 'Best First'.
Re: need help on SED
by blokhead (Monsignor) on May 27, 2003 at 16:30 UTC
    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

Re: need help on SED
by TomDLux (Vicar) on May 27, 2003 at 16:44 UTC

    Instead of using sed, go to CPAN and check out my module Text-ASED, the Advanced Stream EDitor.

    It allowes you to edit files without the cost of invoking an external process. It's particularly useful if you need to perform the same set of edits on several files, even more so if you perform mostly-the-same edits, with minor variations, on various files.

Re: need help on SED
by TVSET (Chaplain) on May 27, 2003 at 16:18 UTC
    You'd better edit your post to use <CODE>...</CODE> tags around your code. Otherwise it looks ugly and doubt really many people will look into it. If you expect an effort, you should show one yourself.

    Leonid Mamtchenkov aka TVSET

      i didn't know about codes , i'll use them
Re: need help on SED
by Aristotle (Chancellor) on May 29, 2003 at 22:45 UTC

    Strange. Why invoke sed out of a Perl script to do a job Perl is perfectly capable to do itself? I also see a lot of horribly named $tempfoo variables that have nothing to do with the system call in question, but no indication as to where $search, $replace, $File1, and $Replace_html get populated and what kind of values they might contain.

    If you want good advice, you should probably take a step back and explain to us exactly what it is you that want to do.

    Makeshifts last the longest.

Re: need help on SED
by wufnik (Friar) on May 27, 2003 at 16:23 UTC
    dearie me plz. use a "<CODE>" tag to begin you code and an end code tag to end your code, even if it is sed.

    but it sure does not look like sed to me. it looks more like "troll"