Current location: Hot Scripts Forums » Programming Languages » Perl » Read from growing file and write to a file


Read from growing file and write to a file

Reply
  #1 (permalink)  
Old 07-31-10, 01:08 PM
gurupn12481 gurupn12481 is offline
New Member
 
Join Date: Jul 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Read from growing file and write to a file

Hi,

Can anyone please help me on the below issue?

I have a input log file that will be growing. I need to read this input file and print the last record whenever certain pattern matches (pls remember that input file is dynamic - other files will be writing to it). I'm not able to write to the output file but print it on STDOUT. Code piece is below:

#!/usr/bin/perl
use strict;
use IO::Handle;
open(IN, "posfile.txt"); ## this file will be growing
for (; {
while (<IN>) {
my @array = split /\|/;
if (eof) {
open(OUT, ">posoutfile.txt") or die "cant open file\n";
print OUT $_;
#print $_;
}
IN->clearerr();
}
}

The above code displays output on STDOUT, but not to the output file (OUT Filehandle).

Thanks is advance.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-21-11, 10:27 AM
wickedxter wickedxter is offline
New Member
 
Join Date: Apr 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Code:
#!/usr/bin/perl
use strict;
use IO::Handle;

open(OUT,">>posoutfile.txt");  #open output file for appending so multi writes can happen
open(IN, "posfile.txt");    ## this file will be growing open input for reading
while (<IN>){    # read each line of posfile.txt
 ($line) = split(/\|/,$_); # split each line with the |
 print OUT $line if ($line =~ //); # match using a regex to capture what ur lookin for and print to OUT
}
close IN;
close OUT;
hope this helps or if not let me know.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks

Tags
perl


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
form to save or read file from server junoflydog JavaScript 1 09-12-09 08:07 AM
Can javascript be used to read and write to a text file kept on a webserver? gmb1994 JavaScript 4 11-23-04 11:50 AM
Database file or object file is read only, w_inaim ASP 2 09-21-04 03:18 PM
write to beginning of file or reverse an array of binary data abtimoteo Perl 1 01-20-04 11:09 AM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 05:28 PM


All times are GMT -5. The time now is 06:56 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.