LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-08-2007, 05:04 AM   #1
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Rep: Reputation: Disabled
[Perl] append to new file (Solved)


I can print the output of a file to STDOUT but I want to append it to a new file. Here's my code

Code:
#!/usr/bin/perl -w
my $file="file.log";
my $nf="nf.log";
open (FILE, "$file") or die ("can't open this: $!");
my @lines=<FILE>; 
print @lines;
close (FILE) or die "FILE can't be closed: $!";

Last edited by noir911; 02-08-2007 at 05:13 AM.
 
Old 02-08-2007, 05:15 AM   #2
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Original Poster
Rep: Reputation: Disabled
Not sure if it's "efficient" but I found this script [1] which helped me to solve this issue. Here's the new solved code

Code:
#!/usr/bin/perl -w

my $file="file.log";
my $nf="nf.log";

open (FILE, "$file") or die ("can't open this: $!");

 my @lines=<FILE>;

close (FILE) or die "FILE can't be closed: $!";
open (NF, ">> $nf");
foreach $line (@lines) {
print NF $line;
}
close NF;
exit;
[1] http://www.linuxquestions.org/questi...ad.php?t=54267
 
Old 02-08-2007, 05:20 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

To write/append to a file you need to open another filehandle (for $nf in your case).

Something like this:

open NEWFILE, ">$nf" or die "Can't open $nf";

To write to this filehandle:

print NEWFILE @lines;

You also need to close it again (close NEWFILE; ).

To append to an already existing file, use >> instead of >.

Your example code would look like this:
Code:
#!/usr/bin/perl -w
my $file="file.log";
my $nf="nf.log";
open NEWFILE, ">$nf" or die "Can't open $nf";
open (FILE, "$file") or die ("can't open this: $!");
my @lines=<FILE>; 
print NEWFILE @lines;
close (FILE) or die "FILE can't be closed: $!";
close NEWFILE;
Hope this helps.
 
Old 02-08-2007, 05:36 AM   #4
noir911
Member
 
Registered: Apr 2004
Location: Baltimore, MD
Posts: 681

Original Poster
Rep: Reputation: Disabled
Thanks. It's neat and small - only 9 lines!
 
Old 02-08-2007, 07:29 AM   #5
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Code:
close (FILE) or die "FILE can't be closed: $!";
close NEWFILE;
I have few considerations with the above!

Say if the filehandler FILE is not closed properly, necessarily NEWFILE handler also will not be closed which I think can be avoided.
 
Old 02-08-2007, 07:38 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

I never use the 'or die' part in the close statement, but the OP does/did.

You are correct by stating that if close FILE fails, the close NEWFILE is never reached. But if the script stops, the file is (probably) closed anyway. I say probably, cause I've never actually had this problem myself.

All in all you are correct in warning the OP (and others), and a better way would be:
Code:
close FILE;
close NEWFILE;
 
  


Reply

Tags
perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ append data to file blizunt7 Programming 3 07-04-2005 01:47 AM
> writes a new file, how to append? luwigie Linux - General 1 08-26-2004 09:42 PM
cgi perl : I cant get perl to append my html file... the_y_man Programming 3 03-22-2004 06:07 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM
append to file d-rez Linux - Newbie 2 06-20-2002 05:55 AM


All times are GMT -5. The time now is 07:51 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration