Operating System - HP-UX
1820643 Members
1971 Online
109626 Solutions
New Discussion юеВ

UNIX COMMAND TO TRIM LOG FILES

 
SOLVED
Go to solution
alana duvall_1
Occasional Contributor

UNIX COMMAND TO TRIM LOG FILES

I am looking for a command to trim log files that will keep the most recent entries (say the last 1000 lines). I tried using the tail command: tail -1000 logfile > logfile
But that did not work. It would only work for a number not larger than 200 lines.

Any suggestions?

Thanks
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: UNIX COMMAND TO TRIM LOG FILES

Hi Tony:

You can usw 'wc' to calculate the number of lines in your file, and then compute the line number from which you want to "save forward". For instance:

# LINES=`wc -l < myfile`
# let LINES=$LINES-100 # to remove the first 100 lines...
# sed -n $LINES,'$p' myfile > myfile.trimmed

...JRF...
KapilRaj
Honored Contributor

Re: UNIX COMMAND TO TRIM LOG FILES

Hi ,

it's very easy.

#tail -l 1000 "logfile" > newlog
#cp newlog logfile

best of luck

kaps
Nothing is impossible
alana duvall_1
Occasional Contributor

Re: UNIX COMMAND TO TRIM LOG FILES

Kaps

Thanks for your input. When I try your solution I only get a maximum of 229 lines in the trimmed file. The original file has over 8000 lines.
Volker Borowski
Honored Contributor

Re: UNIX COMMAND TO TRIM LOG FILES

Rob Smith
Respected Contributor

Re: UNIX COMMAND TO TRIM LOG FILES

Hi, try this script:#!/usr/bin/sh
##
### Utility Menu to Manage the System Log
####
#####
syslog=/var/adm/syslog/syslog.log
## cp $syslog $syslog.orig

clear
print -n "

Manage the System Log: $syslog

1. View --> Read the System Log File
2. Count --> Tally the number of ERRORS and WARNINING
3. Trim --> Empty the Log File
4. Display --> Show Size of the System Log File
5. Exit --> Exit this Program

Select from the menu: "
read pick

case "$pick" in
1| [Vv] ) tail -200 $syslog | more ; manage ;;
2| [Cc] ) errors=$(grep -c -e ERROR $syslog)
warns=$(grep -c -e WARNING $syslog)
((total=errors+warns))
print "There are $total Errors or Warnings in the $syslog" ;;
3| [Tt] ) print "Clearing the $syslog"
cp -p $syslog /var/adm/syslog/syslog.log.$(date +%m%d%y)
> $syslog ;;
4| [Dd] ) ls -l /var/adm/syslog/syslog.log | awk '{print $5}' ;;

5| [Ee] ) exit ;;
esac
exit

Hope this helps.

Rob


Learn the rules so you can break them properly.
alana duvall_1
Occasional Contributor

Re: UNIX COMMAND TO TRIM LOG FILES

JRF

Thank you
I have not used the sed command before. It looks like it will take care of what I need
Thanks Again