Find replace : Regular expression

Discussion in 'Perl Misc' started by Subra, Jun 12, 2007.

  1. Subra

    Subra Guest

    Hi,

    I have to replace the strings "LINUX", "__linux__", "linux" with
    "__linux". Please help me to correct the below script.

    My input file is of type below

    #ifdef LINUX
    DO SOME THING
    #end if

    #ifdef __linux
    DO SOME THING
    #endif

    #ifdef linux
    DO SOME THING
    #endif

    #ifdef __linux__
    DO SOME THING
    #endif


    My o/p file should be

    #ifdef __linux
    DO SOME THING
    #end if

    #ifdef __linux
    DO SOME THING
    #endif

    #ifdef __linux
    DO SOME THING
    #endif

    #ifdef __linux
    DO SOME THING
    #endif

    My script (Which needs correction)
    #!/tools/opt/bin/perl
    $fileCount = 0;
    while($fileCount <= @ARGV-1)
    {
    open(FileHandle,$ARGV[$fileCount]) || die ("File not present
    $ARGV[$fileCount]");
    open(OpFile,">OpFile.$ARGV[$fileCount]") || die ("OpFile not
    present");
    while($line=<FileHandle>)
    {
    #$line=~s/(^#.*)LINUX(.*)/$1__linux$2/g;
    $line=~s/(^#.*)linux(.*)/$1__linux$2/g;
    #$line=~s/(^#.*)__linux__(.*)/$1__linux$2/g;

    print OpFile $line;
    }
    close(FileHandle);
    $fileCount++;
    }
    Subra, Jun 12, 2007
    #1
    1. Advertising

  2. Subra

    Subra Guest

    I think the below script works
    $fileCount = 0;
    while($fileCount <= @ARGV-1)
    {
    open(FileHandle,$ARGV[$fileCount]) || die ("File not present
    $ARGV[$fileCount]");
    open(OpFile,">OpFile.$ARGV[$fileCount]") || die ("OpFile not
    present");
    while($line=<FileHandle>)
    {
    $line=~s/(^#.*)__linux__(.*)/$1__linux$2/g;
    $line=~s/(^#.*)LINUX(.*)/$1__linux$2/g;
    $line=~s/(^#.*)[\s]+linux(.*)/$1 __linux $2/g;

    print OpFile $line;
    }
    close(FileHandle);
    $fileCount++;
    }
    Subra, Jun 12, 2007
    #2
    1. Advertising

  3. Subra wrote:
    >
    > $line=~s/(^#.*)__linux__(.*)/$1__linux$2/g;
    > $line=~s/(^#.*)LINUX(.*)/$1__linux$2/g;
    > $line=~s/(^#.*)[\s]+linux(.*)/$1 __linux $2/g;


    That could possibly be written:

    $line =~ s/(^#.*?)(?:__)?linux(?:__)?/$1__linux/gi;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 12, 2007
    #3
  4. On Jun 11, 10:41 pm, Subra <> wrote:
    > Hi,
    >
    > I have to replace the strings "LINUX", "__linux__", "linux" with
    > "__linux". Please help me to correct the below script.


    Maybe try this:

    [sshaw@localhost ruby]$ perl -i.og -pe'/^#ifdef/ && s/(?:__)?
    linux(?:__)?/__linux__/i' file1 file2

    file1, file2 will contain the changes. The original files will be save
    with a .og extension

    I say /^#ifdef/ just in case you are really using `perl -P` on your
    source files, <:^|
    On another note, has anyone ever used the preprocessor with their perl
    programs?
    Skye Shaw!@#$, Jun 12, 2007
    #4
  5. Subra

    Ala Qumsieh Guest

    Gunnar Hjalmarsson wrote:

    > Subra wrote:
    >>
    >> $line=~s/(^#.*)__linux__(.*)/$1__linux$2/g;
    >> $line=~s/(^#.*)LINUX(.*)/$1__linux$2/g;
    >> $line=~s/(^#.*)[\s]+linux(.*)/$1 __linux $2/g;

    >
    > That could possibly be written:
    >
    > $line =~ s/(^#.*?)(?:__)?linux(?:__)?/$1__linux/gi;


    you meant this:

    $line =~ s/(^#.*?)(?:__)?linux(?:__)?/${1}__linux/gi;

    the OP had it wrong too, and I suspect you just copied/pasted.

    --Ala
    Ala Qumsieh, Jun 13, 2007
    #5
  6. Ala Qumsieh wrote:
    > Gunnar Hjalmarsson wrote:
    >> Subra wrote:
    >>> $line=~s/(^#.*)__linux__(.*)/$1__linux$2/g;
    >>> $line=~s/(^#.*)LINUX(.*)/$1__linux$2/g;
    >>> $line=~s/(^#.*)[\s]+linux(.*)/$1 __linux $2/g;

    >> That could possibly be written:
    >>
    >> $line =~ s/(^#.*?)(?:__)?linux(?:__)?/$1__linux/gi;

    >
    > you meant this:
    >
    > $line =~ s/(^#.*?)(?:__)?linux(?:__)?/${1}__linux/gi;
    >
    > the OP had it wrong too, and I suspect you just copied/pasted.


    Well, no, to be honest I didn't think of it, but I had tested the code
    before posting.

    Now, when I play with various alternatives, it seems as if the braces
    are only needed if the string following the dollar-digit variable begins
    with a digit.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 13, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    420
  2. VSK
    Replies:
    2
    Views:
    2,266
  3. lucky
    Replies:
    0
    Views:
    322
    lucky
    Feb 9, 2005
  4. J. VerSchave

    regular expression search replace

    J. VerSchave, Feb 28, 2004, in forum: Java
    Replies:
    2
    Views:
    404
    Alan Moore
    Feb 29, 2004
  5. TOXiC
    Replies:
    3
    Views:
    1,011
    Frederic Rentsch
    Feb 3, 2007
Loading...

Share This Page