Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   appending to a file. (http://www.velocityreviews.com/forums/t911268-appending-to-a-file.html)

Nene 08-31-2009 07:58 PM

appending to a file.
 
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

Randal L. Schwartz 08-31-2009 08:13 PM

Re: appending to a file.
 
>>>>> "Nene" == Nene <rodbass63@gmail.com> 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
<merlyn@stonehenge.com> <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

Peter J. Holzer 08-31-2009 08:17 PM

Re: appending to a file.
 
On 2009-08-31 19:58, Nene <rodbass63@gmail.com> 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


J. Gleixner 08-31-2009 08:20 PM

Re: appending to a file.
 
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"



Uri Guttman 08-31-2009 08:25 PM

Re: appending to a file.
 
>>>>> "N" == Nene <rodbass63@gmail.com> 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 ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Nene 08-31-2009 09:13 PM

Re: appending to a file.
 
On Aug 31, 4:25*pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> >>>>> "N" == Nene *<rodbas...@gmail.com> 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 *------ *u...@stemsystems.com *-------- *http://www.sysarch.com--
> ----- *Perl Code Review , Architecture, Development, Training, Support ------
> --------- *Gourmet Hot Cocoa Mix *---- *http://bestfriendscocoa.com---------



Jürgen Exner 08-31-2009 11:34 PM

Re: appending to a file.
 
Nene <rodbass63@gmail.com> 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

Nene 09-17-2009 05:39 PM

Re: appending to a file.
 
On Aug 31, 4:13*pm, mer...@stonehenge.com (Randal L. Schwartz) wrote:
> >>>>> "Nene" == Nene *<rodbas...@gmail.com> 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>Listen80Listen5555Listen443Listen7170Listen80 02Listen8005
> Nene>Listen8008Listen8009Listen8010Listen8011Liste n8013Listen8022
> Nene>Listen8023Listen8024Listen8045Listen8050Liste n8051Listen8102
> Nene>Listen8108Listen8155Listen8110Listen8210Liste n8211Listen8263
> Nene>Listen8122Listen8145Listen8346Listen8347Liste n8147Listen8148
> 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
> <mer...@stonehenge.com> <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 09-19-2009 12:41 PM

Re: appending to a file.
 
On Aug 31, 4:13*pm, mer...@stonehenge.com (Randal L. Schwartz) wrote:
> >>>>> "Nene" == Nene *<rodbas...@gmail.com> 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>Listen80Listen5555Listen443Listen7170Listen80 02Listen8005
> Nene>Listen8008Listen8009Listen8010Listen8011Liste n8013Listen8022
> Nene>Listen8023Listen8024Listen8045Listen8050Liste n8051Listen8102
> Nene>Listen8108Listen8155Listen8110Listen8210Liste n8211Listen8263
> Nene>Listen8122Listen8145Listen8346Listen8347Liste n8147Listen8148
> 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
> <mer...@stonehenge.com> <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

sln@netherlands.com 09-19-2009 01:08 PM

Re: appending to a file.
 
On Sat, 19 Sep 2009 05:41:47 -0700 (PDT), Nene <rodbass63@gmail.com> wrote:

>On Aug 31, 4:13*pm, mer...@stonehenge.com (Randal L. Schwartz) wrote:
>> >>>>> "Nene" == Nene *<rodbas...@gmail.com> 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


All times are GMT. The time now is 08:07 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.