unix command required

  • Follow


hello,

I have a log file which I do not want it to go past 5000 lines. Is
there a tail command that would delete the oldest lines in a log files
while pushing the last one. A type of FILO (first in, First out)
method in unix.

k
0
Reply lerameur (21) 6/17/2008 9:48:19 PM

On Tue, 17 Jun 2008 14:48:19 -0700, lerameur wrote:

> I have a log file which I do not want it to go past 5000 lines. Is there
> a tail command that would delete the oldest lines in a log files while
> pushing the last one. A type of FILO (first in, First out) method in
> unix.

Removing a single line from the start of a file is a massive job, usually 
requiring reading and writing the whole of the rest of the file.

The normal thing to do is to rotate your log files, so when one gets to a 
certain size, you move it to another name and start a new one.  If you 
rotate at, say 1000 lines and keep the last four files, you will always 
have between 4000-5000 lines of log.

How you do this depends on if you are writing an application or a script.

An alternative is to use fixed length lines and a fixed size file, and 
write to it as a circle buffer, which will mean that the lines are out of 
order, but if they include the date then that's probably ok.

viza
0
Reply tom.viza2 (51) 6/17/2008 11:52:20 PM


On 2008-06-17, lerameur <lerameur@yahoo.com> wrote:
>
>
> hello,
>
> I have a log file which I do not want it to go past 5000 lines. Is
> there a tail command that would delete the oldest lines in a log files
> while pushing the last one. A type of FILO (first in, First out)
> method in unix.
>
> k

while true; do
  tail -f -n +0 logfile | awk 'NR>5000{exit}'
  tail -n 5000 logfile >newfile
  mv newfile logfile
done

Using an array, it may be possible to eliminate the second tail and do 
the whole thing in awk, with a pipe from the program that creates the 
log.  It might be more efficient if you delete a number of lines when 
the count reaches 5000, or keep the last 5000 lines when the number of 
lines reaches 5000 + x.
0
Reply marcumbill (1012) 6/17/2008 11:58:45 PM

lerameur wrote:
> 
> hello,
> 
> I have a log file which I do not want it to go past 5000 lines. Is
> there a tail command that would delete the oldest lines in a log files
> while pushing the last one. A type of FILO (first in, First out)
> method in unix.
> 
> k

Did you look at "man alog"?

D.J.D.
0
Reply djesys.no (1536) 7/4/2008 3:39:01 PM

3 Replies
16 Views

(page loaded in 0.082 seconds)


Reply: