in place edit.

Discussion in 'Perl Misc' started by Nene, Nov 2, 2009.

  1. Nene

    Nene Guest

    I used the same code below (provided by Randy) to do in place edits on
    a config file and it works great, but I when I try to use the same
    code for an html file, it does not work.

    ###Below is the code###

    #!/usr/bin/perl -w
    use strict;
    use warnings;
    my $stuff = "</tr>";

    my $a = "insert_text_here\n";
    {
    local @ARGV = "misc.txt";
    local $^I = ".bak"; # appended to the backup copy
    while (<>) {
    if ((/$stuff/..!/$stuff/) =~ /E/) { # if we're at the end of
    the Listens
    $_ = $a . $_; # prepend the line to the next line
    }
    print; # but print whatever we have
    }
    }




    ###Below is the result of the script when I run it#



    <tr>
    <td>http://xxx.xxx.xxx.xxx</td>
    <td>
    <a href='https://xxx:8002'>
    https://xxx.xxx.xxx.xxx:8002
    </a>
    </td>
    </tr>
    insert_text_here
    <tr>
    <td>http://xxx.xxx.xxx.xxx</td>
    <td>
    <a href='https://xxx.xxx:8010'>
    https://xxx.xxx.xxx:8010
    </a>
    </td>
    </tr>
    insert_text_here
    <tr>
    <td>http://xxx.xxx.xxx.xxx:8145</td>
    <td>
    <a href='https://xxx.xxx.xxx:8148'>
    https://xxx.xxx.xxx:8148
    </a>
    </td>
    </tr>
    insert_text_here


    Text in this line
    Text in this line
    Text in this line
    ####

    As you can see "insert_text_here" is appended at the bottom of every
    "</tr>"; I only want it appended to the last "</tr>".
    If this is not possible, is there a module that will read a specific
    number of lines backwards and then insert?
    Nene, Nov 2, 2009
    #1
    1. Advertising

  2. Nene

    Guest

    On Mon, 2 Nov 2009 06:39:21 -0800 (PST), Nene <> wrote:

    >I used the same code below (provided by Randy) to do in place edits on
    >a config file and it works great, but I when I try to use the same
    >code for an html file, it does not work.
    >
    >###Below is the code###
    >
    >###Below is the result of the script when I run it#
    >
    >As you can see "insert_text_here" is appended at the bottom of every
    >"</tr>"; I only want it appended to the last "</tr>".
    >If this is not possible, is there a module that will read a specific
    >number of lines backwards and then insert?


    Something like this.. where you adapt it to create a new file.

    use strict;
    use warnings;

    my $line_to_insert = "insert\n";
    my $last;
    open DATA, '<data.txt' or die "can't open data.txt: $!";

    {
    while (<DATA>) {
    $last = tell(DATA) if /<\/tr>/;
    }
    if (defined $last) {
    seek DATA,0,0;
    while (<DATA>) {
    print;
    print $line_to_insert if tell(DATA) == $last;
    }
    }
    }
    close DATA;

    __END__

    -sln
    , Nov 2, 2009
    #2
    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. dba56
    Replies:
    0
    Views:
    584
    dba56
    Jul 7, 2006
  2. Laurent Julliard

    [Q] In place edit mode activation?

    Laurent Julliard, Jul 9, 2008, in forum: Ruby
    Replies:
    1
    Views:
    84
    Joel VanderWerf
    Jul 10, 2008
  3. Randy Harris

    IN PLACE edit syntax help needed

    Randy Harris, Feb 23, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    99
    Joe Smith
    Feb 25, 2004
  4. khan
    Replies:
    7
    Views:
    593
  5. Nick Tong

    Joseph Scotts Edit in place work.

    Nick Tong, Nov 14, 2006, in forum: Javascript
    Replies:
    1
    Views:
    113
    Nick Tong
    Nov 15, 2006
Loading...

Share This Page