appending to a file.

Discussion in 'Perl Misc' started by Nene, Aug 31, 2009.

  1. Nene

    Nene Guest

    Greetings,

    I want to open an apache file and append a line at the bottom of all
    the Listen entries, not the bottom of the file. For example, I want to
    append once it finds the last Listen entry in the file. For example,
    the last Listen entry is 'Listen 8150', I want to append 'Listen 8151'
    underneath Listen 8150. From what I read, I might have to use a
    module, but I'm trying not install modules. Is it possible?

    Listen 80
    Listen 5555
    Listen 443
    Listen 7170
    Listen 8002
    Listen 8005
    Listen 8008
    Listen 8009
    Listen 8010
    Listen 8011
    Listen 8013
    Listen 8022
    Listen 8023
    Listen 8024
    Listen 8045
    Listen 8050
    Listen 8051
    Listen 8102
    Listen 8108
    Listen 8155
    Listen 8110
    Listen 8210
    Listen 8211
    Listen 8263
    Listen 8122
    Listen 8145
    Listen 8346
    Listen 8347
    Listen 8147
    Listen 8148
    Listen 8146
    Listen 8150
     
    Nene, Aug 31, 2009
    #1
    1. Advertising

  2. >>>>> "Nene" == Nene <> writes:

    Nene> Greetings, I want to open an apache file and append a line at the bottom
    Nene> of all the Listen entries, not the bottom of the file. For example, I
    Nene> want to append once it finds the last Listen entry in the file. For
    Nene> example, the last Listen entry is 'Listen 8150', I want to append
    Nene> 'Listen 8151' underneath Listen 8150. From what I read, I might have to
    Nene> use a module, but I'm trying not install modules. Is it possible?

    Nene> Listen 80 Listen 5555 Listen 443 Listen 7170 Listen 8002 Listen 8005
    Nene> Listen 8008 Listen 8009 Listen 8010 Listen 8011 Listen 8013 Listen 8022
    Nene> Listen 8023 Listen 8024 Listen 8045 Listen 8050 Listen 8051 Listen 8102
    Nene> Listen 8108 Listen 8155 Listen 8110 Listen 8210 Listen 8211 Listen 8263
    Nene> Listen 8122 Listen 8145 Listen 8346 Listen 8347 Listen 8147 Listen 8148
    Nene> Listen 8146 Listen 8150

    Use "in-place-edit" mode:

    my $line_to_insert = "Listen 8151\n";

    {
    local @ARGV = "/path/to/your/httpd.conf";
    local $^I = ".bak"; # appended to the backup copy

    while (<>) {

    if ((/^Listen/..!/^Listen/) =~ /e/) { # if we're at the end of the Listens
    $_ = $line_to_insert . $_; # prepend the line to the next line
    }
    print; # but print whatever we have
    }

    }

    print "Just another Perl hacker,"; # the original

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
     
    Randal L. Schwartz, Aug 31, 2009
    #2
    1. Advertising

  3. On 2009-08-31 19:58, Nene <> wrote:
    > I want to open an apache file and append a line at the bottom of all
    > the Listen entries, not the bottom of the file. For example, I want to
    > append once it finds the last Listen entry in the file. For example,
    > the last Listen entry is 'Listen 8150', I want to append 'Listen 8151'
    > underneath Listen 8150. From what I read, I might have to use a
    > module, but I'm trying not install modules. Is it possible?


    No, you don't need a module (You never *need* a module. Modules make
    things simpler because you can just use them instead of writing the code
    yourself). But you can't insert something in the middle of a file. What
    you can do is:

    1) Read the file into memory (either as a string or an array of lines).

    2) Search for the correct position and Insert the line "Listen 8150\n".

    3) Write the new file.

    (The module File::Slurp makes step 1 and 3 easier, but both are only a
    few lines anyway)

    hp
     
    Peter J. Holzer, Aug 31, 2009
    #3
  4. Nene

    J. Gleixner Guest

    Nene wrote:
    > Greetings,
    >
    > I want to open an apache file and append a line at the bottom of all
    > the Listen entries, not the bottom of the file. For example, I want to
    > append once it finds the last Listen entry in the file. For example,
    > the last Listen entry is 'Listen 8150', I want to append 'Listen 8151'
    > underneath Listen 8150. From what I read, I might have to use a
    > module, but I'm trying not install modules. Is it possible?
    >
    > Listen 80

    [...]
    > Listen 8150


    You want to insert a line after some condition is true.

    perldoc -q "How do I change, delete, or insert a line in a file"
     
    J. Gleixner, Aug 31, 2009
    #4
  5. Nene

    Uri Guttman Guest

    >>>>> "N" == Nene <> writes:

    N> I want to open an apache file and append a line at the bottom of all
    N> the Listen entries, not the bottom of the file. For example, I want to
    N> append once it finds the last Listen entry in the file. For example,
    N> the last Listen entry is 'Listen 8150', I want to append 'Listen 8151'
    N> underneath Listen 8150. From what I read, I might have to use a
    N> module, but I'm trying not install modules. Is it possible?

    that isn't appending, but inserting. please use the right term.

    as for not using modules, that is silly and it has been discussed many
    time. if you have an isp who doesn't install modules, change isps or
    install them yourself. if you have a boss that doesn't allow them,
    change jobs. if this is homework (which usually doesn't allow modules)
    then say so and show perl code that we can help with.

    otherwise there are still questions. do you always know the line where
    the insert is to go? is the file sorted (it seems partially sorted from
    your data)? how large is this file? (i doubt you are listening to
    massive numbers of sockets.

    there are several approaches and some can be done without modules. read
    the file in line by line, print out each line to a new file and when you
    get to the desired line, print the inserted line and the print the
    rest. you can do this on the existing file with the -i option or to a
    new file and rename it to the old one. you can use Tie::File and do this
    in a simple loop. you can use File::Slurp and also do this in a simple
    loop and even faster. in all cases you have to read in the whole file
    and write it out again with the inserted line. on most file systems
    today you can't directly insert lines in the middle of a file.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Aug 31, 2009
    #5
  6. Nene

    Nene Guest

    On Aug 31, 4:25 pm, "Uri Guttman" <> wrote:
    > >>>>> "N" == Nene  <> writes:

    >
    >   N> I want to open an apache file and append a line at the bottom of all
    >   N> the Listen entries, not the bottom of the file. For example, I want to
    >   N> append once it finds the last Listen entry in the file. For example,
    >   N> the last Listen entry is 'Listen 8150', I want to append 'Listen 8151'
    >   N> underneath Listen 8150. From what I read, I might have to use a
    >   N> module, but I'm trying not install modules. Is it possible?
    >
    > that isn't appending, but inserting. please use the right term.
    >
    > as for not using modules, that is silly and it has been discussed many
    > time. if you have an isp who doesn't install modules, change isps or
    > install them yourself. if you have a boss that doesn't allow them,
    > change jobs.


    Uri, you made me laugh on this one. The economy and job market is bad
    in Florida, I'll put up with the boss and not install modules.

    if this is homework (which usually doesn't allow modules)
    > then say so and show perl code that we can help with.
    >
    > otherwise there are still questions. do you always know the line where
    > the insert is to go? is the file sorted (it seems partially sorted from
    > your data)? how large is this file? (i doubt you are listening to
    > massive numbers of sockets.
    >
    > there are several approaches and some can be done without modules. read
    > the file in line by line, print out each line to a new file and when you
    > get to the desired line, print the inserted line and the print the
    > rest. you can do this on the existing file with the -i option or to a
    > new file and rename it to the old one. you can use Tie::File and do this
    > in a simple loop. you can use File::Slurp and also do this in a simple
    > loop and even faster. in all cases you have to read in the whole file
    > and write it out again with the inserted line. on most file systems
    > today you can't directly insert lines in the middle of a file.
    >
    > uri
    >
    > --
    > Uri Guttman  ------    --------  http://www.sysarch.com--
    > -----  Perl Code Review , Architecture, Development, Training, Support ------
    > ---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com---------
     
    Nene, Aug 31, 2009
    #6
  7. Nene <> wrote:
    >I want to open an apache file


    What is an apache file?

    >and append a line at the bottom of all
    >the Listen entries, not the bottom of the file. For example, I want to
    >append once it finds the last Listen entry in the file. For example,
    >the last Listen entry is 'Listen 8150', I want to append 'Listen 8151'
    >underneath Listen 8150. From what I read, I might have to use a
    >module, but I'm trying not install modules. Is it possible?


    Which in your example below happens to be the last line in the file, so
    you could just as well append to the end of the file.

    [sample data snipped]

    jue
     
    Jürgen Exner, Sep 1, 2009
    #7
  8. Nene

    Nene Guest

    On Aug 31, 4:13 pm, (Randal L. Schwartz) wrote:
    > >>>>> "Nene" == Nene  <> writes:

    >
    > Nene> Greetings, I want to open an apache file and append a line at the bottom
    > Nene> of all theListenentries, not the bottom of the file. For example, I
    > Nene> want to append once it finds the lastListenentry in the file. For
    > Nene> example, the lastListenentry is 'Listen8150', I want to append
    > Nene> 'Listen8151' underneathListen8150. From what I read, I might have to
    > Nene> use a module, but I'm trying not install modules. Is it possible?
    >
    > Nene>Listen80Listen5555Listen443Listen7170Listen8002Listen8005
    > Nene>Listen8008Listen8009Listen8010Listen8011Listen8013Listen8022
    > Nene>Listen8023Listen8024Listen8045Listen8050Listen8051Listen8102
    > Nene>Listen8108Listen8155Listen8110Listen8210Listen8211Listen8263
    > Nene>Listen8122Listen8145Listen8346Listen8347Listen8147Listen8148
    > Nene>Listen8146Listen8150
    >
    > Use "in-place-edit" mode:
    >
    >     my $line_to_insert = "Listen8151\n";
    >
    >     {
    >       local @ARGV = "/path/to/your/httpd.conf";
    >       local $^I = ".bak"; # appended to the backup copy
    >
    >       while (<>) {
    >
    >         if ((/^Listen/..!/^Listen/) =~ /e/) { # if we're at theend of the Listens
    >           $_ = $line_to_insert . $_; # prepend the line to the next line
    >         }
    >         print; # but print whatever we have
    >       }
    >
    >     }
    >
    > print "Just another Perl hacker,"; # the original
    >
    > --
    > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    > <> <URL:http://www.stonehenge.com/merlyn/>
    > Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    > Seehttp://methodsandmessages.vox.com/for Smalltalk and Seaside discussion


    Mr. Schwartz, this works great, thank you very much.
     
    Nene, Sep 17, 2009
    #8
  9. Nene

    Nene Guest

    On Aug 31, 4:13 pm, (Randal L. Schwartz) wrote:
    > >>>>> "Nene" == Nene  <> writes:

    >
    > Nene> Greetings, I want to open an apache file and append a line at the bottom
    > Nene> of all theListenentries, not the bottom of the file. For example, I
    > Nene> want to append once it finds the lastListenentry in the file. For
    > Nene> example, the lastListenentry is 'Listen8150', I want to append
    > Nene> 'Listen8151' underneathListen8150. From what I read, I might have to
    > Nene> use a module, but I'm trying not install modules. Is it possible?
    >
    > Nene>Listen80Listen5555Listen443Listen7170Listen8002Listen8005
    > Nene>Listen8008Listen8009Listen8010Listen8011Listen8013Listen8022
    > Nene>Listen8023Listen8024Listen8045Listen8050Listen8051Listen8102
    > Nene>Listen8108Listen8155Listen8110Listen8210Listen8211Listen8263
    > Nene>Listen8122Listen8145Listen8346Listen8347Listen8147Listen8148
    > Nene>Listen8146Listen8150
    >
    > Use "in-place-edit" mode:
    >
    >     my $line_to_insert = "Listen8151\n";
    >
    >     {
    >       local @ARGV = "/path/to/your/httpd.conf";
    >       local $^I = ".bak"; # appended to the backup copy
    >
    >       while (<>) {
    >
    >         if ((/^Listen/..!/^Listen/) =~ /e/) { # if we're at theend of the Listens
    >           $_ = $line_to_insert . $_; # prepend the line to the next line
    >         }
    >         print; # but print whatever we have
    >       }
    >
    >     }
    >
    > print "Just another Perl hacker,"; # the original
    >
    > --
    > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    > <> <URL:http://www.stonehenge.com/merlyn/>
    > Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    > Seehttp://methodsandmessages.vox.com/for Smalltalk and Seaside discussion


    Umm, I thought it worked but it's not working, probably because I'm
    missing something. I created a new file and it has:

    Listen 80
    Listen 81
    Listen 82

    But when I run:

    #!/usr/bin/perl -w
    use strict;

    my $line_to_insert = "Listen 8151\n";
    {
    local @ARGV = "/home/control/misc_perl_scripts/httpd.conf";
    local $^I = ".bak"; # appended to the backup copy
    while (<>) {
    if ((/^Listen/..!/^Listen/) =~ /e/) { # if we're at the end of
    the Listens
    $_ = $line_to_insert . $_; # prepend the line to the next
    line
    }
    print; # but print whatever we have
    }
    }


    __END__

    It doesn't print the $line_to_insert, and it doesn't print anything,
    the file remains the same? Any clue?

    Nene
     
    Nene, Sep 19, 2009
    #9
  10. Nene

    Guest

    On Sat, 19 Sep 2009 05:41:47 -0700 (PDT), Nene <> wrote:

    >On Aug 31, 4:13 pm, (Randal L. Schwartz) wrote:
    >> >>>>> "Nene" == Nene  <> writes:

    >>

    >Umm, I thought it worked but it's not working, probably because I'm
    >missing something. I created a new file and it has:
    >
    >Listen 80
    >Listen 81
    >Listen 82
    >
    >But when I run:
    >
    >#!/usr/bin/perl -w
    >use strict;
    >
    >my $line_to_insert = "Listen 8151\n";
    > {
    > local @ARGV = "/home/control/misc_perl_scripts/httpd.conf";
    > local $^I = ".bak"; # appended to the backup copy
    > while (<>) {
    > if ((/^Listen/..!/^Listen/) =~ /e/) { # if we're at the end of

    ^
    E

    >the Listens
    > $_ = $line_to_insert . $_; # prepend the line to the next
    >line
    > }
    > print; # but print whatever we have
    > }
    > }
    >
    >
    >__END__
    >
    >It doesn't print the $line_to_insert, and it doesn't print anything,
    >the file remains the same? Any clue?
    >
    >Nene


    -sln
     
    , Sep 19, 2009
    #10
  11. Nene

    Nene Guest

    On Sep 19, 9:08 am, wrote:
    > On Sat, 19 Sep 2009 05:41:47 -0700 (PDT), Nene <> wrote:
    > >On Aug 31, 4:13 pm, (Randal L. Schwartz) wrote:
    > >> >>>>> "Nene" == Nene  <> writes:

    >
    > >Umm, I thought it worked but it's not working, probably because I'm
    > >missing something. I created a new file and it has:

    >
    > >Listen80
    > >Listen81
    > >Listen82

    >
    > >But when I run:

    >
    > >#!/usr/bin/perl -w
    > >use strict;

    >
    > >my $line_to_insert = "Listen8151\n";
    > >    {
    > >      local @ARGV = "/home/control/misc_perl_scripts/httpd.conf";
    > >      local $^I = ".bak"; # appended to the backup copy
    > >      while (<>) {
    > >        if ((/^Listen/..!/^Listen/) =~ /e/) { # if we're at the end of

    >
    >                                          ^
    >                                          E
    >
    > >the Listens
    > >          $_ = $line_to_insert . $_; # prepend the line to the next
    > >line
    > >        }
    > >        print; # but print whatever we have
    > >      }
    > >    }

    >
    > >__END__

    >
    > >It doesn't print the $line_to_insert, and it doesn't print anything,
    > >the file remains the same? Any clue?

    >
    > >Nene

    >
    > -sln- Hide quoted text -
    >
    > - Show quoted text -


    I used a captial E install of a small case e, it still doesn't work.
    if ((/^Listen/..!/^Listen/) =~ /E/) { # if we're at the end of

    Did I undertand the last post correctly?

    Nene
     
    Nene, Sep 21, 2009
    #11
  12. Nene

    Guest

    On Mon, 21 Sep 2009 10:39:34 -0700 (PDT), Nene <> wrote:

    >On Sep 19, 9:08 am, wrote:
    >> On Sat, 19 Sep 2009 05:41:47 -0700 (PDT), Nene <> wrote:
    >> >On Aug 31, 4:13 pm, (Randal L. Schwartz) wrote:
    >> >> >>>>> "Nene" == Nene  <> writes:

    >>
    >> >Umm, I thought it worked but it's not working, probably because I'm
    >> >missing something. I created a new file and it has:

    >>
    >> >Listen80
    >> >Listen81
    >> >Listen82

    >>
    >> >But when I run:

    >>
    >> >#!/usr/bin/perl -w
    >> >use strict;

    >>
    >> >my $line_to_insert = "Listen8151\n";
    >> >    {
    >> >      local @ARGV = "/home/control/misc_perl_scripts/httpd.conf";
    >> >      local $^I = ".bak"; # appended to the backup copy
    >> >      while (<>) {
    >> >        if ((/^Listen/..!/^Listen/) =~ /e/) { # if we're at the end of

    >>
    >>                                          ^
    >>                                          E
    >>
    >> >the Listens
    >> >          $_ = $line_to_insert . $_; # prepend the line to the next
    >> >line
    >> >        }
    >> >        print; # but print whatever we have
    >> >      }
    >> >    }

    >>
    >> >__END__

    >>
    >> >It doesn't print the $line_to_insert, and it doesn't print anything,
    >> >the file remains the same? Any clue?

    >>
    >> >Nene

    >>
    >> -sln- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    >I used a captial E install of a small case e, it still doesn't work.
    >if ((/^Listen/..!/^Listen/) =~ /E/) { # if we're at the end of
    >
    >Did I undertand the last post correctly?
    >
    >Nene
    >


    Works for me. Post your data file and code.
    -sln
    -------------

    out.txt before:

    start
    Listen80
    Listen 5555
    Listen 443
    insert above me
    more stuff

    out.txt after:

    start
    Listen80
    Listen 5555
    Listen 443
    Listen 8151
    insert above me
    more stuff

    -------------

    use strict;
    use warnings;

    my $line_to_insert = "Listen 8151\n";
    {
    local @ARGV = "out.txt";
    local $^I = ".bak"; # appended to the backup copy
    while (<>) {
    if ((/^Listen/..!/^Listen/) =~ /E/) { # if we're at the end of the Listens
    $_ = $line_to_insert . $_; # prepend the line to the next line
    }
    print; # but print whatever we have
    }
    }
     
    , Sep 21, 2009
    #12
  13. Nene

    Nene Guest

    On Sep 21, 2:33 pm, wrote:
    > On Mon, 21 Sep 2009 10:39:34 -0700 (PDT), Nene <> wrote:
    > >On Sep 19, 9:08 am, wrote:
    > >> On Sat, 19 Sep 2009 05:41:47 -0700 (PDT), Nene <> wrote:
    > >> >On Aug 31, 4:13 pm, (Randal L. Schwartz) wrote:
    > >> >> >>>>> "Nene" == Nene  <> writes:

    >
    > >> >Umm, I thought it worked but it's not working, probably because I'm
    > >> >missing something. I created a new file and it has:

    >
    > >> >Listen80
    > >> >Listen81
    > >> >Listen82

    >
    > >> >But when I run:

    >
    > >> >#!/usr/bin/perl -w
    > >> >use strict;

    >
    > >> >my $line_to_insert = "Listen8151\n";
    > >> >    {
    > >> >      local @ARGV = "/home/control/misc_perl_scripts/httpd.conf";
    > >> >      local $^I = ".bak"; # appended to the backup copy
    > >> >      while (<>) {
    > >> >        if ((/^Listen/..!/^Listen/) =~ /e/) { # if we're atthe end of

    >
    > >>                                          ^
    > >>                                          E

    >
    > >> >the Listens
    > >> >          $_ = $line_to_insert . $_; # prepend the line to the next
    > >> >line
    > >> >        }
    > >> >        print; # but print whatever we have
    > >> >      }
    > >> >    }

    >
    > >> >__END__

    >
    > >> >It doesn't print the $line_to_insert, and it doesn't print anything,
    > >> >the file remains the same? Any clue?

    >
    > >> >Nene

    >
    > >> -sln- Hide quoted text -

    >
    > >> - Show quoted text -

    >
    > >I used a captial E install of a small case e, it still doesn't work.
    > >if ((/^Listen/..!/^Listen/) =~ /E/) { # if we're at the end of

    >
    > >Did I undertand the last post correctly?

    >
    > >Nene

    >
    > Works for me. Post your data file and code.
    > -sln
    > -------------
    >
    > out.txt before:
    >
    > start
    > Listen80Listen5555Listen443
    > insert above me
    > more stuff
    >
    > out.txt after:
    >
    > start
    > Listen80Listen5555Listen443Listen8151
    > insert above me
    > more stuff
    >
    > -------------
    >
    >     use strict;
    >     use warnings;
    >
    >     my $line_to_insert = "Listen8151\n";
    >     {
    >       local @ARGV = "out.txt";
    >       local $^I = ".bak"; # appended to the backup copy
    >       while (<>) {
    >         if ((/^Listen/..!/^Listen/) =~ /E/) { # if we're at theend of the Listens
    >           $_ = $line_to_insert . $_; # prepend the line to the next line
    >         }
    >         print; # but print whatever we have
    >       }
    >     }- Hide quoted text -
    >
    > - Show quoted text -


    Thank you sln, it's working. It was a permission issue.
    Nene

    Thanks Mr.Schwartz.
     
    Nene, Sep 21, 2009
    #13
    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. scopp
    Replies:
    3
    Views:
    444
    Guest
    Jan 22, 2004
  2. Marcelo
    Replies:
    5
    Views:
    10,476
    Gordon Beaton
    Oct 25, 2005
  3. Red Ogden
    Replies:
    0
    Views:
    1,113
    Red Ogden
    Jul 16, 2003
  4. Thomas Heller

    appending data to an xml file

    Thomas Heller, Jan 11, 2005, in forum: Python
    Replies:
    1
    Views:
    361
    Peter Hansen
    Jan 12, 2005
  5. Greg McIntyre
    Replies:
    0
    Views:
    451
    Greg McIntyre
    Sep 12, 2005
Loading...

Share This Page