Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Inplace editing the elegant way

0 views
Skip to first unread message

jrpfinch

unread,
Oct 9, 2008, 10:16:54 AM10/9/08
to
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ürgen Exner

unread,
Oct 9, 2008, 11:16:00 AM10/9/08
to

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

Hartmut Camphausen

unread,
Oct 10, 2008, 1:23:06 PM10/10/08
to
Jürgen Exner schrieb:

> 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


--
------------------------------------------------
Hartmut Camphausen h.camp[bei]textix[punkt]de

Jürgen Exner

unread,
Oct 10, 2008, 1:40:51 PM10/10/08
to
Hartmut Camphausen <Jus...@somewhere.de> wrote:
>Jürgen Exner schrieb:
>
>> 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.

Nice idea!

jue

s...@netherlands.com

unread,
Oct 12, 2008, 2:06:40 PM10/12/08
to

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

sln

0 new messages