Advertisement
eocanha

filter-time

Mar 5th, 2021
892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.88 KB | None | 0 0
  1. #!/bin/bash
  2. START_TIME=$1
  3. END_TIME=$2
  4.  
  5. if [ -n "$START_TIME" ]
  6. then
  7.  STARTED=0
  8.  if [ -n "$END_TIME" ]
  9.  then
  10.   while read LINE
  11.   do
  12.    if [[ ( "$STARTED" = "1" && ! "$LINE" =~ ^[0-9].*$ ) || ( "$LINE" =~ ^[0-9].*$ && "$LINE" > "$START_TIME" && "$LINE" < "$END_TIME" ) ]]
  13.    then
  14.     STARTED=1
  15.     echo "$LINE"
  16.    else
  17.     if [[ "$STARTED" = "1" ]]
  18.     then
  19.      # This prints the line matching the end time
  20.      echo "$LINE"
  21.     fi
  22.     STARTED=0
  23.    fi
  24.   done
  25.  else
  26.   while read LINE
  27.   do
  28.    if [[ ( "$STARTED" = "1" && ! "$LINE" =~ ^[0-9].*$ ) || ( "$LINE" =~ ^[0-9].*$ && "$LINE" > "$START_TIME" ) ]]
  29.    then
  30.     STARTED=1
  31.     echo "$LINE"
  32.    else
  33.     STARTED=0
  34.    fi
  35.   done
  36.  fi
  37. else
  38. cat >&2 << EOF
  39. This script filters log lines between start and (optionally) end times.
  40. Usage: $0 START_TIME [ END_TIME ]
  41. Examples: $0 0:00:05 0:00:06
  42.           $0 0:00:05
  43. EOF
  44. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement