Inplace editing the elegant way

J

jrpfinch

I would like to develop a script that can inplace edit a large file
(potentially 1Gb), omitting some lines according to a regex and
replacing some lines according to another regex. Here is an example
of a script that does this with a couple of hardcoded patterns:

{
local ($^I, @ARGV) = ('', ($full_path_to_config_file));
while (<>)
{
s/this/that/;
s/foo/bar/;
print unless ((/delete me/) or (/Delete Me Too/));
}
}

What is the best way to scale this for n patterns? When I say best, I
mean a good balance of speed and readability. Can I put all the
omission patterns in an array and all the search/replace patterns in
another array and somehow wedge them into the example above?

Any help appreciated.

Jon
 
J

Jürgen Exner

jrpfinch said:
I would like to develop a script that can inplace edit a large file
(potentially 1Gb), omitting some lines according to a regex and
replacing some lines according to another regex. Here is an example
of a script that does this with a couple of hardcoded patterns:

{
local ($^I, @ARGV) = ('', ($full_path_to_config_file));
while (<>)
{
s/this/that/;
s/foo/bar/;
print unless ((/delete me/) or (/Delete Me Too/));
}
}

What is the best way to scale this for n patterns? When I say best, I
mean a good balance of speed and readability. Can I put all the
omission patterns in an array and all the search/replace patterns in
another array and somehow wedge them into the example above?

I'd probably store the REs pattern and replacement text as key/value
pairs in a hash(*) and just loop through them.
And a separate hash or array for the "delete" patterns.

*: that should work, because REs are just strings, too.

jue
 
H

Hartmut Camphausen

Jürgen Exner said:
I'd probably store the REs pattern and replacement text as key/value
pairs in a hash(*) and just loop through them.
And a separate hash or array for the "delete" patterns.

*: that should work, because REs are just strings, too.

I'd consider to store the /compiled/ patterns + replacement strings in
an array - if efficiency is an issue.

Else each pattern would have been recompiled for each chunk of data.


jm2p + mfg, Hartmut
 
J

Jürgen Exner

Hartmut Camphausen said:
I'd consider to store the /compiled/ patterns + replacement strings in
an array - if efficiency is an issue.

Nice idea!

jue
 
S

sln

I would like to develop a script that can inplace edit a large file
(potentially 1Gb), omitting some lines according to a regex and
replacing some lines according to another regex. Here is an example
of a script that does this with a couple of hardcoded patterns:

{
local ($^I, @ARGV) = ('', ($full_path_to_config_file));
while (<>)
{
s/this/that/;
s/foo/bar/;
print unless ((/delete me/) or (/Delete Me Too/));
}
}

What is the best way to scale this for n patterns? When I say best, I
mean a good balance of speed and readability. Can I put all the
omission patterns in an array and all the search/replace patterns in
another array and somehow wedge them into the example above?

Any help appreciated.

Jon

This is fine for simple line based parsing.
How would you handle replacements that span lines?

sln
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top