For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > Adding text in file written in Perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Adding text in file written in Perl
Starr Corbin

2007-08-07, 7:00 pm

Hello, I have written a Perl script that creates the dhcpd.conf and /
etc/hosts file for Red Hat Linux. The data is derived from a single
text file. What is stumping me is how to insert an automatic text
line for just the /etc/hosts part of my script that would
automatically insert a text line at the top of the /etc/hosts file
each time the script is run.

For instance, I want to make sure that at the top of my /etc/hosts
file it reads:
127.0.0.0 localhost.localhostdomain localhost

I have tried using "push" and "print", but have not made it work. How
can I get my 127.0.0.0 to appear at the top of my /etc/hosts/ file
every time I run the script to create it?

Thank you!!

Mr. Shawn H. Corey

2007-08-07, 7:00 pm

starr.corbin@gmail.com wrote:
> How
> can I get my 127.0.0.0 to appear at the top of my /etc/hosts/ file
> every time I run the script to create it?


Rewrite the entire file. Most file systems are not set up to allow you prepend to a file.

--
Just my 0.00000002 million dollars worth,
Shawn

"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
Paul Lalli

2007-08-07, 7:00 pm

On Aug 7, 10:06 am, starr.cor...@gmail.com (Starr Corbin) wrote:
> Hello, I have written a Perl script that creates the dhcpd.conf and /
> etc/hosts file for Red Hat Linux. The data is derived from a single
> text file. What is stumping me is how to insert an automatic text
> line for just the /etc/hosts part of my script that would
> automatically insert a text line at the top of the /etc/hosts file
> each time the script is run.
>
> For instance, I want to make sure that at the top of my /etc/hosts
> file it reads:
> 127.0.0.0 localhost.localhostdomain localhost
>
> I have tried using "push" and "print", but have not made it work. How
> can I get my 127.0.0.0 to appear at the top of my /etc/hosts/ file
> every time I run the script to create it?


I don't understand your question. You're saying you have a script
that creates this file. So... create the file the way you want it.
Wherever you're first putting data into the file, modify your script
at some point previous to that so that it prints the 127.0.0.1 first.

What's the real issue here? Please post a short-but-complete script
that demonstrates the issue you're having.

Paul Lalli

Jeff Pang

2007-08-07, 10:01 pm



-----Original Message-----
>From: starr.corbin@gmail.com
>Sent: Aug 7, 2007 10:06 PM
>To: beginners@perl.org
>Subject: Adding text in file written in Perl
>
>Hello, I have written a Perl script that creates the dhcpd.conf and /
>etc/hosts file for Red Hat Linux. The data is derived from a single
>text file. What is stumping me is how to insert an automatic text
>line for just the /etc/hosts part of my script that would
>automatically insert a text line at the top of the /etc/hosts file
>each time the script is run.
>
>For instance, I want to make sure that at the top of my /etc/hosts
>file it reads:
>127.0.0.0 localhost.localhostdomain localhost
>
>I have tried using "push" and "print", but have not made it work. How
>can I get my 127.0.0.0 to appear at the top of my /etc/hosts/ file
>every time I run the script to create it?
>


Maybe I understand you not correctly.but I think it's not hard to do it.

1. write that line to hosts file at first,
open HD,">","/etc/hosts" or die $!;
print HD "127.0.0.0 localhost.localhostdomain localhost\n";
close HD;

2. wirte the other lines to hosts file then,
open HD,">>","/etc/hosts" or die $!;
print HD @other_lines;
close HD;

Good luck.

--
Jeff Pang <pangj@earthlink.net>
http://home.arcor.de/jeffpang/
Jeff Pang

2007-08-07, 10:01 pm



-----Original Message-----
>From: Jeff Pang <pangj@earthlink.net>
>
>1. write that line to hosts file at first,
>open HD,">","/etc/hosts" or die $!;
>print HD "127.0.0.0 localhost.localhostdomain localhost\n";
>close HD;
>
>2. wirte the other lines to hosts file then,
>open HD,">>","/etc/hosts" or die $!;
>print HD @other_lines;
>close HD;
>


sorry it just can be written together,

open HD,">","/etc/hosts" or die $!;
print HD "127.0.0.0 localhost.localhostdomain localhost\n";
print HD @other_lines;
close HD;


--
Jeff Pang <pangj@earthlink.net>
http://home.arcor.de/jeffpang/
Srinivas

2007-08-08, 4:00 am

Jeff Pang wrote:
> -----Original Message-----
>
>
> Maybe I understand you not correctly.but I think it's not hard to do it.
>
> 1. write that line to hosts file at first,
> open HD,">","/etc/hosts" or die $!;
> print HD "127.0.0.0 localhost.localhostdomain localhost\n";
>

Make it 127.0.0.1
> close HD;
>
> 2. wirte the other lines to hosts file then,
> open HD,">>","/etc/hosts" or die $!;
> print HD @other_lines;
> close HD;
>
> Good luck.
>
> --
> Jeff Pang <pangj@earthlink.net>
> http://home.arcor.de/jeffpang/
>
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com