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

hello everyone I am using sed in my code to replace a few things like so system qq{sed -e 's/$search/$replace/g' $write > $write2}; it works great and gives me the desired results, but the tricky part is to make it case insensitive can anyone add some additions and help me out thank you

Replies are listed 'Best First'.
Re: how to make sed case INsensitive
by jdporter (Paladin) on Jun 06, 2003 at 20:33 UTC
    Well, considering this is perlmonks, not sedmonks, I can give you a solution that happens to use perl intrinsically rather than spawning sed.
    open F1, "< $write" or die $!; open F2, "> $write2" or die $!; while (<F1>) { s/$search/$replace/ig; print F2 $_; } close F1; close F2;
    Depending on what you're doing, you might be able to avoid the external files altogether.

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

Re: how to make sed case INsensitive
by chromatic (Archbishop) on Jun 06, 2003 at 20:31 UTC

    Did you know that Perl can do just about everything sed can do? A straight-across port might be:

    system qq|perl -e 's/$search/$replace/gi' $write > $write2|;

    I'd likely do it in-process, though.

Re: how to make sed case INsensitive
by runrig (Abbot) on Jun 06, 2003 at 21:01 UTC
    Though the other answers are probably more desirable, just for comparison, here's what you actually asked for (untested):
    # You may also want additional substitutions # to backwhack sed metacharacters, e.g. # $search =~ s/\./\\./g; $search =~ s/([[:alpha:]])/"[".lc($1).uc($1)."]"/ge; system "sed -e s/$search/$replace/g $write1 > $write2";
    But it's much easier to use the case insensitive regex switch in perl :-)
    Update: Updated code. In reply to AM: I'm just creating sed character classes, which, e.g., replaces "a" with "[aA]" to make it case insensitive.
      can u explain me what u did to $search please thank you
        It's a waste of a process and computing time to do it that way and creates a needless dependency on an external tool. Why do you insist on sed?

        Makeshifts last the longest.