opening the file at a specific line number.

N

Nene

Hello,

I have a script that runs every 5 minutes and greps for errors in a
log file. Througout the day, the log file gets bigger and bigger. Is
there a way to open the file from a specific line number. My objective
is that when the scripts runs, it will will get the last line number
of the file, when it runs again in 5 minutes, I want it to start from
the last line from the last time the script ran.

I checked FAQ, they had articles similar to what I'm looking for but
according to FAQ "How do I change, delete, or insert a line in a file,
or append to the beginning of a file?" It said, "Perl doesn't provide
random access to lines (especially since the record input separator,
$/ , is mutable)..."

Any help will be greatly appreciated.

Nene
 
S

smallpond

Hello,

I have a script that runs every 5 minutes and greps for errors in a
log file. Througout the day, the log file gets bigger and bigger. Is
there a way to open the file from a specific line number. My objective
is that when the scripts runs, it will will get the last line number
of the file, when it runs again in 5 minutes, I want it to start from
the last line from the last time the script ran.

I checked FAQ, they had articles similar to what I'm looking for but
according to FAQ "How do I change, delete, or insert a line in a file,
or append to the beginning of a file?" It said, "Perl doesn't provide
random access to lines (especially since the record input separator,
$/ , is mutable)..."

Any help will be greatly appreciated.

Nene

You can either leave the file open and keep reading, or tell your
position and seek to it after reopening. Both are explained in
this doc:

perldoc -q tail
How do I do a "tail -f" in perl?
 
J

Jürgen Exner

Nene said:
I have a script that runs every 5 minutes and greps for errors in a
log file. Througout the day, the log file gets bigger and bigger. Is
there a way to open the file from a specific line number.
No.

My objective
is that when the scripts runs, it will will get the last line number
of the file, when it runs again in 5 minutes, I want it to start from
the last line from the last time the script ran.

But you can use seek() to jump directly to a specific location in the
file.

jue
 
C

C.DeRykus

Hello,

I have a script that runs every 5 minutes and greps for errors in a
log file. Througout the day, the log file gets bigger and bigger. Is
there a way to open the file from a specific line number. My objective
is that when the scripts runs, it will will get the last line number
of the file, when it runs again in 5 minutes, I want it to start from
the last line from the last time the script ran.

I checked FAQ, they had articles similar to what I'm looking for but
according to FAQ "How do I change, delete, or insert a line in a file,
or append to the beginning of a file?" It said, "Perl doesn't provide
random access to lines (especially since the record input separator,
$/ , is mutable)..."

Any help will be greatly appreciated.

If the log entries are timestamped, you
could use File::ReadBackwards to read
entries bacward from the last line to
the timestamp of the previous cron entry.
 
H

Henry Law

Nene said:
Hello,

I have a script that runs every 5 minutes and greps for errors in a
log file.

If you really mean simple grep'ing for strings then I wonder if it might
be easier to write it as a shell script. Each time you grep write the
number of lines to a file (something like wc -l > transient_file) and
then when you wake up the next time do a bit of arithmetic with the
current state of the file and the previous size in "transient_file" to
give a starting line number.
Then you could use "tail -$STARTING_LINE_NUMBER" to leave you with just
the new part and grep that.

Not to be discussed further in this group, I suppose!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top