Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a log file and would like to cut it into copies of 15min. In the log file the lines start with:

dd mmm yyyy hh:mm:ss,xxx 

e.g.

12 Feb 2013 16:05:02,xxx log text...

and the file rotates every hour so I would like to take a copy of the log file, split it into 15min of logging and have say 4 log files.

share|improve this question
1  
cut doesn't support completely regexr , So use awk.Also cut have probelm with space.awk solved it. –  Mohsen Pahlevanzadeh Feb 14 '13 at 4:05
2  
You might check cronosplit to split the files out. –  JodieC Feb 14 '13 at 4:15
    
For obvious reasons, being able to split into 20-minute pieces would make this a lot easier. (In case that 15-minutes requirement isn't set in stone.) –  Ulrich Schwarz Feb 14 '13 at 5:33
    
A regex for grep could select a range of time from a log. But you will have to adjust it for each time range, for each day. Scripting this would give you more iterative control over the lines you process. Let me suggest Python. –  invert Feb 14 '13 at 6:08

2 Answers 2

Maybe this one does the job:

INNAME="krasser.log";
OUTNAME="speck.log";
cat $INNAME | grep -v '^$' |
while read r; do
  LOG_NUM=$(($(echo $r | cut -d' ' -f4 | cut -d':' -f2 | cut -d',' -f1)/15));
  [ "$r" ] && echo $r >> $OUTNAME.$LOG_NUM;
done

cronosplit is probably the way to go. but if you need a fast "one-time"-solution. you'll maybe happier with the "one-liner".

share|improve this answer

AWK:

cat input |
    awk -F ':' '{if ($2 < 15) {print $0 > "15.log"} else print}' |
    awk -F ':' '{if ($2 < 30) {print $0 > "30.log"} else print}' |
    awk -F ':' '{if ($2 < 45) {print $0 > "45.log"} else print}' |
    cat > 60.log
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.