473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

reading from line X in perl

Ciary
247 Expert 100+
hi all,

i recently encountered a problem about something that should be really easy but in fact i couldn't find any information on how to do it.
what i want to do is read a logfile in perl. i open the file, read the data, parse it, then close the file. so far so good.
now, while the script is running, i append a few lines to the logfile. i now want perl to somehow remember at which line it last read, and when i open the file i want to jump to the end and read only the new lines.
the reason for this is that i might have huge logfiles and reading them completely all the time won't work fast enough.
Expand|Select|Wrap|Line Numbers
  1. $exitfile = '/path/to/the_exit_file';
  2. while (!(-e $exitfile)) {
  3.     print "and we read?\n";
  4.  
  5.     open(DAT, "/path/to/mylog.log") || die("Could not open file!");    
  6.     foreach $line (<DAT>) {
  7.         chomp($line);
  8.         print $line;
  9.     }   
  10.     close(DAT);
  11.     sleep(1);
  12. }
  13.  
in this case, i keep opening the file every second and reading it all the way through until i create a certain file (the exit file)
anyone know how i can just read the new lines?
what i was thinking was counting the lines in my foreach loop, but then i have the problem of not being able to move my pointer to that location.
thanks!
Nov 7 '11 #1

✓ answered by Ciary

forget it, searched for 3 hours, posted this, then 10 minutes later i had a solutions

Expand|Select|Wrap|Line Numbers
  1. $exitfile = '/path/to/the_exit_file';
  2. my $pointerPos = 0;
  3. while (!(-e $exitfile)) {
  4.     print "and we read?\n";
  5.  
  6.     open(DAT, "/path/to/mylog.log") || die("Could not open file!");    
  7.     seek DAT, $pointerPos, 0;
  8.     foreach $line (<DAT>) {
  9.         print $line;
  10.     }   
  11.     $pointerPos = tell DAT;
  12.     close(DAT);
  13.     sleep(1);
  14. }
  15.  
just "tell" to ask the position in bytes and "seek" to move the pointer back to that position. also the last 0 in seek means you start $pointerPos byes from the START of the file. you can also put seek from the end of the file.

1 2720
Ciary
247 Expert 100+
forget it, searched for 3 hours, posted this, then 10 minutes later i had a solutions

Expand|Select|Wrap|Line Numbers
  1. $exitfile = '/path/to/the_exit_file';
  2. my $pointerPos = 0;
  3. while (!(-e $exitfile)) {
  4.     print "and we read?\n";
  5.  
  6.     open(DAT, "/path/to/mylog.log") || die("Could not open file!");    
  7.     seek DAT, $pointerPos, 0;
  8.     foreach $line (<DAT>) {
  9.         print $line;
  10.     }   
  11.     $pointerPos = tell DAT;
  12.     close(DAT);
  13.     sleep(1);
  14. }
  15.  
just "tell" to ask the position in bytes and "seek" to move the pointer back to that position. also the last 0 in seek means you start $pointerPos byes from the START of the file. you can also put seek from the end of the file.
Nov 7 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Andreas | last post by:
Hi, I'm just developing a .net (compact) application and therefore make use of the StreamReader class - and especially the ReadLine function. Now, when I try to read lines from a text file that...
20
by: plmanikandan | last post by:
Hi, I need to read a file line by line.each line contains different number of characters.I opened file using fopen function.is there any function to read the file line by line Regards, Mani
5
by: loveme | last post by:
i am reading a file line by line...But in some case some exception comes,i catch exception but program not exceute next line.Van u please tell how skip the exception comes on line and exceute next...
8
by: bahoo | last post by:
Hi, I have a text file containing a single line of text, such as 0024 How should I read it into a "list"? I tried this, but the "join" did not work as expected. Any suggestions?
1
by: dxz | last post by:
I have a perl script to run on both UNIX and Windows. How should I write the Shabang line: On UNIX, it is #!/usr/bin/perl On Windows, it is #!C:\perl\bin\perl.exe Which one should I use?...
3
by: xyz | last post by:
Hi, I have a text file around 7GB includes 100 million lines... I want to read the data line by line when I approach my module.. ie., when i read for the first time , my program shuld read only...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.