Home > Software design >  Bash script to extract lines between two different timestamps in a log file
Bash script to extract lines between two different timestamps in a log file

Time:06-26

I want to extract lines between two different timestamps, I've written a bash script, but it doesn't work properly; please let me know How I can do it correctly.

My log file looks like this:

22/06/23 19:30:21        [Logs...]
22/06/24 17:58:30        [Logs...]
22/06/24 17:59:48        [Logs...]
22/06/24 18:11:27        [Logs...]
22/06/24 18:11:28        [Logs...]

the code which is not working properly:

command: ./test.sh -t 22/06/24,17:58:30-22/06/24,18:11:27

Code:

#! /bin/bash
    #INPUT: 22/06/24,17:58:00-22/06/24,18:11:27
    #startDate=22/06/24
    startDate=$(echo $2 | cut -d "-" -f 1 | cut -d "," -f 1)
    
    #endDate=22/06/24
    endDate=$(echo $2 | cut -d "-" -f 2 | cut -d "," -f 1)
    
    #startTime=17:58:00
    startTime=$(echo $2 | cut -d "-" -f 1 | cut -d "," -f 2)
    
    #endTime=18:11:30
    endTime=$(echo $2 | cut -d "-" -f 2 | cut -d "," -f 2)
    
    #Script Parameter Format to search in Log Files: YY/MM/DD hh:mm:ss-YY/MM/DD hh:mm:ss
    
    #timestampStart=22-06-24 17:58:00
    timestampStart=$(echo $startDate | cut -d "/" -f 1)-$(echo $startDate | cut -d "/" -f 2)-$(echo $startDate | cut -d "/" -f 3)" "$startTime
    
    #timestampEnd=22-06-24 18:11:27
    timestampEnd=$(echo $endDate | cut -d "/" -f 1)-$(echo $endDate | cut -d "/" -f 2)-$(echo $endDate | cut -d "/" -f 3)" "$endTime
    
    #tstart=1656077280
    tStart=`date --date="$timestampStart"  %s`
    
    #tEnd=1656078090
    tEnd=`date --date="$timestampEnd"  %s`
    
    while read -r line; do
      re="[0-9]{2}\/[0-9]{2}\/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}"
      if [[ $line =~ $re ]]; then
    
        #searchDate="22/06/24 17:58:00"
        searchDate=$(echo $line | cut -d " " -f 1,2)
    
        #searchTime="22-06-24 17:58:00"
        searchTime=$(echo $searchDate | cut -d "/" -f 1)-$(echo $searchDate | cut -d "/" -f 2)-$(echo $searchDate | cut -d "/" -f 3)
        #tSearch=1656077280
        tSearch=`date --date="$searchTime"  %s`
      fi
    
      #if 1656077280>=1656077280 AND 1656077280<=1656078090
      if  [ $tSearch -gt $tStart ] && [ $tSearch -lt $tEnd ];then
    
        #22/06/24 17:58:00       at java.lang.Integer.parseInt(Integer.java:542)
        echo $line
      fi
    done < /oracleAS10g/product/opmn/logs/OC4J~officeauto~default_island~1

Output Error:

./test.sh: line 43: [: -gt: unary operator expected

Expected output: shows the lines between two timestamps that are specified

22/06/24 17:58:30        [Logs...]
22/06/24 17:59:48        [Logs...]
22/06/24 18:11:27        [Logs...]

CodePudding user response:

Looks like a simple sed would do the job here:

filter() { sed -n "\~$1~,\~$2~p" file.log; }

filter "22/06/24 17:58:30" "22/06/24 18:11:27"

22/06/24 17:58:30        [Logs...]
22/06/24 17:59:48        [Logs...]
22/06/24 18:11:27        [Logs...]
  • Related