Skip to content

Instantly share code, notes, and snippets.

@chernjie
Last active August 29, 2015 14:03
Show Gist options
  • Save chernjie/47e25bc3666c00d62d32 to your computer and use it in GitHub Desktop.
Save chernjie/47e25bc3666c00d62d32 to your computer and use it in GitHub Desktop.
Split log file by date
#!/bin/bash
function breakup()
{
LAST=$(wc -l "$1" | xargs | cut -d' ' -f1)
LAST=$(($LAST + 1))
grep -no ^201.-..-.. "$1" | sort -u -t: -k2 | sort -t: -rnk1 | while read j
do
DATE=$(echo $j | cut -d: -f2)
LINE=$(echo $j | cut -d: -f1)
echo "$1-${DATE//-/}"
sed -n $LINE,$(($LAST - 1))p "$1" > "$1-${DATE//-/}"
LAST=$LINE
done
}
for file
do
breakup $file
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment