Hydrosine
Hydrosine

Reputation: 145

perl replace block of text

I'm very stuck on creating a piece of perl i need.

I have a file which contains the following

    define(`CONFM4_AST_MODULES', `NOLOAD => app_thingie.so
    NOLOAD => app_thingie2.so
    NOLOAD => app_thingie3.so')

Now i need to be able to rewrite this whole block so i was thinking on matching everything that is between "(`CONFM4_AST_MODULES" and "')" Delete it and then append it again. on the end of the file. Is it possible to regex a thing like this? if so how?

It is not possible to hardcode the entire text in my perl script as it changes sometimes.

If you have any other ideas on how to solve this i love to hear it :)

also the number of thingies that exist in the file changes from 3 to 20.

Upvotes: 2

Views: 1453

Answers (1)

choroba
choroba

Reputation: 241968

Yes, it is possible. You can read the whole file into a variable and do the replacement:

my $var = do { local $/; <> };                           # Read in the file.
my $re = qr/(define\(`CONFM4_AST_MODULES',[^)]+\)\n?)/;  # How to search for the defines.
my @defs = $var =~ /$re/g;                               # Remember the defines.
$var =~ s/$re//g;                                        # Remove them.
print $var, @defs;

Updated: Code improved and tested.

Upvotes: 2

Related Questions