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

monk,

sed -e 's/\(CELL_NAME\)\(.*\)\(_[A-Z]_\)\(.*\)/\1\2_\4/g'
what is the equivalent regular expression in perl. I would like to read a big file and changes the contents
FROM : AAK6615B__S_KhushkKhairMFaqir
TO : AAK6615B__KhushkKhairMFaqir

Replies are listed 'Best First'.
Re: regular expression question
by citromatik (Curate) on Aug 07, 2007 at 11:05 UTC

    Basically the same, but substitute "\1",etc by "$1"etc...

    perl -e '$kk="AAK6615B__S_KhushkKhairMFaqir";$kk=~s/(.*)_[A-Z]_(.*)/$1 +_$2/;print "$kk\n"'

    Prints:

    AAK6615B__KhushkKhairMFaqir

    citromatik

      ...because \1 (etc) is strictly for regexps. The replacement expression uses $1 (etc).
      $var =~ m/...\1.../; $var =~ s/...\1.../...$1.../;
Re: regular expression question
by casiano (Pilgrim) on Aug 07, 2007 at 11:09 UTC
    Try in the command line:
    perl -e 's/(CELL_NAME)(.*)(_[A-Z]_)(.*)/$1$2_$4/g' -p -i.bak yourfile

    that will produce a backup of your file in "yourfile.bak" and make the substitution "ala sed"

    Casiano

Re: regular expression question
by swampyankee (Parson) on Aug 07, 2007 at 13:48 UTC

    There is also s2p, which converts antiquated, horrid sed scripts to modern, delightful Perl programs.


    emc

    Information about American English usage here and here.

    Any New York City or Connecticut area jobs? I'm currently unemployed.