I have a quite large apache logfile (2,5 G). Its that big because i havent used logrotate until now. Now the logs get rotated corretly but i want the rotation (one logfile per month) for the time until now so i have to "rotate" the old logfile.

Does anyone know how i could do that ? (some script that splits my logfile per month ?)

Thanks in advance,


migrated from stackoverflow.com Feb 27 '12 at 3:50

This question came from our site for professional and enthusiast programmers.


Use awk to find the month boundaries, then use csplit to split by context.

sample data:

1-1  ...
1-15 ...
1-30 ...
2-1  ...
2-15 ...
2-29 ...
3-2  ...
3-20 ...

run command

$ cat log | awk -F- 'x!=$1{x=$1; print "MONTH"}1' | csplit - /MONTH/ '{*}'

delete first line

$ sed -i 1d xx*

LoadModule log_rotate_module modules/mod_log_rotate.so

CustomLog site.%m-.access.log


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy