Re: appending to a file.



"N" == Nene <rodbass63@xxxxxxxxx> 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@xxxxxxxxxxxxxxx -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
.