LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Trim Log Files (https://www.linuxquestions.org/questions/linux-server-73/trim-log-files-755703/)

doby48 09-16-2009 02:39 PM

Trim Log Files
 
I log certain activity on my server and I want to trim the files. I know of several ways to do this if I want to keep a running archive, wipe out files etc but what I am looking to do is to keep the most recent 25000 lines and delete the rest. I was going to use $split --lines=25000 but the problem is that the oldest logs are at the beginning of the file so when using split then there may be only one file (aa) or there may be two files (aa and ab). I cant just rename aa as that is the oldest data and I cant rename ab as that file may or may not exists as some logs may be under 25000 lines. Any other thoughts or any ways to use split in reverse order (bottom to top)?

EDIT: the log file I am starting out with would be area_logs.log for example and I want to keep this file running with the latest 25000 lines. In the example above if I ran split and came up with area_logs.log then I would rename the latest file back to this file name. Hopefully that all makes sense as I need the running active log to be the lates entries and anything beyond that can be deleted, no need to archive the data.

doby48 09-16-2009 03:00 PM

Figured this out, I was going about this the wrong way. Using tail I get what I was looking for:
Code:

$tail --lines=25000 area_log.log > area_log_new.log
followed by
Code:

$mv area_log_new.log area_log.log

colucix 09-16-2009 03:23 PM

You can avoid to write a temporary file if you use sed with -i option to edit the file in place. You have only to choose a way to calculate the last line you want to delete, that is the total number of lines minus 25000. Here is two examples:
Code:

$ sed -i "1,$(($(cat area_log.log|wc -l)-25000))d" area_log.log
$ sed -i "1,$(awk 'END{print NR-25000}' area_log.log)d" area_log.log

Edit: I just figured out you can do that using sed commands only:
Code:

sed -i -e :a -e '$q;N;25001,$D;ba' area_log.log
Taken from "USEFUL ONE-LINE SCRIPTS FOR SED" at http://sed.sourceforge.net/sed1line.txt


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