Inplace editing the elegant way

Discussion in 'Perl Misc' started by jrpfinch, Oct 9, 2008.

  1. jrpfinch

    jrpfinch Guest

    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
     
    jrpfinch, Oct 9, 2008
    #1
    1. Advertising

  2. jrpfinch <> wrote:
    >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
     
    Jürgen Exner, Oct 9, 2008
    #2
    1. Advertising

  3. 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
     
    Hartmut Camphausen, Oct 10, 2008
    #3
  4. Hartmut Camphausen <> 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
     
    Jürgen Exner, Oct 10, 2008
    #4
  5. jrpfinch

    Guest

    On Thu, 9 Oct 2008 07:16:54 -0700 (PDT), jrpfinch <> wrote:

    >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
     
    , Oct 12, 2008
    #5
    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. news.verizon.net

    inplace edit error message

    news.verizon.net, Sep 29, 2003, in forum: Perl
    Replies:
    4
    Views:
    1,639
    Jim Gibson
    Oct 8, 2003
  2. msnews.microsoft.com

    DataGrid InPlace Editing

    msnews.microsoft.com, Jan 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    361
    msnews.microsoft.com
    Jan 29, 2005
  3. jason
    Replies:
    0
    Views:
    430
    jason
    Nov 9, 2005
  4. nutty
    Replies:
    6
    Views:
    410
    Tom Widmer
    Jun 15, 2006
  5. Sundaram Ramasamy

    Perl inplace editing

    Sundaram Ramasamy, May 24, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    150
    John W. Krahn
    May 26, 2004
Loading...

Share This Page