Cut Huge Log Files in Half

Ever get those huge log files that you just can not bring into a vi editor, because of buffer overruns of the lack of disk space in /tmp. Cut the file in half.

1) Find the number of lines in the log file:

% wc -l SYSLOG
1234567

2) Divide this number in half

% echo "1234567 / 2" | bc
617283

3) For the first half of the file:

% tail +617283 SYSLOG > SYSLOG.half

For the bottom half of the file:

% tail -617283 SYSLOG > SYSLOG.half

The script may look like this:

#! /bin/sh
#cuthalf - will cut and huge file in half.

FILE=$1

if [ -z "$FILE" ]; then
echo
echo "This program will cut and huge file in half."
echo
echo "Syntax: $0 [file]"
echo
exit 1
fi

SIZE=`cat $FILE | wc -l`
HALF=`echo $SIZE/2 | bc`

echo "Total size = $SIZE Half = $HALF"
tail +${HALF} $FILE > $FILE.cut
ls -al $FILE $FILE.cut

Have a Unix Problem
Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.