UNIX Programming, Certification, System Administration, Performance Tuning Reference Books
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

Return to : Unix System Administration Hints and Tips