I tend to use awk
(with a sed
filter to break your lines up) for things like that:
echo '10:13:55 10:14:00 10:14:01 10:14:02
10:14:41 10:14:46 10:17:58 10:18:00
10:19:10 10:19:16'
| sed -e 's/ *//g' -e 's/^ //' -e 's/ $//' -e 's/ /\n/g'
| awk -F: '
NR==1 {s=$0;s1=$1*3600+$2*60+$3}
NR>1 {t1=$1*3600+$2*60+$3;if (t1-s1 > 30) print s" "$0;s1=t1;s=$0}
'
outputs:
10:14:02 10:14:41
10:14:46 10:17:58
10:18:00 10:19:10
Here's how it works:
- It sets the field separator to
:
for easy extraction.
- When the record number is 1 (
NR==1
), it simply stores the time (s=$0
) and number of seconds since midnight (s1=$1*3600+$2*60+$3
). This is the first baseline.
- Otherwise (
NR>1
), it gets the seconds since midnight (t1=$1*3600+$2*60+$3
) and, if that's more than 30 seconds since the last one, it outputs the last time and this time (if (t1-s1 > 30) print s" "$0
).
- Then it resets the baseline for the next line (
s1=t1;s=$0
).
Keep in mind the sed
command is probably more complicated that it needs to be in this example - it collapses all space sequences to one space, removes them from the start and end of lines then converts newline characters into spaces. Depending on the input form of your data (mine is complicated since it's formatted for readability), this may not all be necessary.
Update: Since the question edit has stated that the input is one time per line, you don't need the sed
part at all.