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

greeting everybody,
I am in need of great help on using sed , i am doing the following line in my perl script
system ("sed -e 's/$c/$b/g' $File1 > $Replace_html");
where value of $c is href="/blah/example.html and the value of $b is href="/dir1/dir2/dir2/new.html" when i run the script it dosen't throw an error but when i check the $Replace_html file which /home/dir/replace.html that file is empty , can anyone help me out with this please
Thank you everyone

update (broquaint): added formatting

Replies are listed 'Best First'.
Re: question on sed
by hardburn (Abbot) on May 15, 2003 at 16:09 UTC

    Why, oh why are you calling sed? Perl has very good regular expression support of its own:

    open(IN, '<', $File1) or die $!; open(OUT, '>', $Replace_html) or die $!; while(my $line = <IN>) { $line =~ s/$c/$b/g; print OUT $line; } close(IN); close(OUT);

    Might be a little longer, but it doesn't rely on an external program. Plus it uses Perl's regular expressions, which are far and away better than sed in almost any way you can think of.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      could if possible tell me why u are using < and > when defining the files, also i ran the code u gave me , i still am not getting output in the output file , could it be coz $c and $b both have "/" in them

        The version above uses the three-element form of open() (available in perl 5.6.0 and later), which is a bit safer than the two-element form. open(FH, '<', $filename) is the same as open(FH, "<$filename"). The problem is that $filename could have a '<' at the beginning, thus changing the meaning of the open(). The three-element form avoids this problem.

        And yes, $c and $b need to have special regex characters escaped. (As broquaint said, variables like $c and $b have special meanings and should be avoided, but let's ignore that for the moment). If '/' is your only problem character, you can use the 'choose your own quotes' feature of Perl's regexen, such as s!$c!$b!g. In this case, the regex substitution will use '!' as a delimiter instead of '/'.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        Note: All code is untested, unless otherwise stated

Re: question on sed
by broquaint (Abbot) on May 15, 2003 at 16:12 UTC
    Update: sed doesn't have alternate delimiters, what was I thinking ...
    You need to change your sed delimiters as they conflict with the slashes in $c and $b (or you could escape them) e.g
    ## warn if we get an abnormal exit status system ("sed -e 's[$c][$b]g' $File1 > $Replace_html") == 0 or warn "sed had problems (exit val $?)\n";
    Make sure you check your sed exit status in case of errors (check out system for details).
    Also beware of using variables called $a or $b due to their automatic global nature (see. sort for more details).

    Try this instead as escaping the slashes should do the trick

    (my $search = $c) =~ s[/](\\/)g; (my $replace = $b) =~ s[/](\\/)g; system("sed -e 's/$search/$replace/g' $File1 > $Replace_html") == 0 warn "sed had problems (exit val $?)\n";
    You also might want to check that $File1 exists and is readable as $Replace_html won't be empty if sed exits normally.
    HTH

    _________
    broquaint

      You have to be careful when using system like you are.
      What would happen if some how $File1 was "myfile ;; rm -rf ."?
      looks like it didn't work , ahhhh i am gonna kill myself,