appending to a file.

N

Nene

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
 
R

Randal L. Schwartz

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
 
P

Peter J. Holzer

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
 
J

J. Gleixner

Nene said:
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"
 
U

Uri Guttman

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
 
N

Nene

  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)
 
J

Jürgen Exner

Nene said:
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
 
N

Nene

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
<[email protected]> <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.
 
N

Nene

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
<[email protected]> <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
 
S

sln

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
 
N

Nene

                                         ^
                                         E



-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
 
S

sln

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
}
}
 
N

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.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top