#!/bin/sh
#
# $Id: syslog-merge,v 0.2 2010/01/17 20:08:54 stef Exp $
#
# Take a bunch of syslog output files and merge them properly sorted in
# chronological order. Each line in the log files must begin with the
# following format string:
# 
# $ date +"%b %e %H:%M:%S"
# Jan 17 21:05:54
#
# Tested with sort(1) from GNU coreutils 6.10.
#
# Copyright (c) 2010, Stefan Pettersson, http://www.bigpointyteeth.se/
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

IFS=
PATH="/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin"
me=$(basename $0)

usage() {
    echo "merge syslog files in chronological order"
    echo "usage: $me <log file> [log file] [...]"
}

error() { echo "$me: $1" 1>&2; }

trap "error 'user abort'; exit 1" 1 2 3 15

if test $# -lt 2; then
    error "bad number of arguments"
    usage
    exit 1
fi

# (1) change dates from "Jan  2" to "Jan 02"
# (2) sort according to date
# (3) undo date changes
cat "$@" |\
sed 's/^\([A-Z][a-z][a-z]\)  \([1-9]\)/\1 0\2/g' |\
sort -M |\
sed 's/^\([A-Z][a-z][a-z]\) 0\([1-9]\)/\1  \2/g'

exit 0
# eof
