Take the 2-minute tour ×

I have a file that contains time-stamped lines of text. I need to search for lines that start at a specific time and end at a specific time. I already have the times needed stored in variables. I need to be able to break up the line and the time into tokens, i believe, and search for tokens that are geq "start time" and leq "stop time". I know this requires a FOR loop, but I'm not sure how to break this up. (Also , I have found that 8 and 9 fail when used for inputs. I am working on this, I think by hr_start+100 and hr_stop+100 before compare and -100 after compare, etc.) Any improvements on this are appreciated. My goal is a completely error-free interface.

echo (Use 24-hour clock)
set /a hr_start=0
set /p hr_start=What hour to start? 
if "%hr_start%"=="b" goto day
if %hr_start% geq 0 (
    if %hr_start% leq 23 (
        if %hr_start% lss 10 set hr_start=0%hr_start%) else (
    echo Invalid hour
    goto hr_start))

set /a min_start=0
set /p min_start=What minute to start? 
if "%min_start%"=="b" goto hr_start
if %min_start% geq 0 (
    if %min_start% leq 59 (
        if %min_start% lss 10 set min_start=0%min_start%) else (
    echo Invalid minute
    goto min_start))

set /a sec_start=0
set /p sec_start=What second to start? 
if "%sec_start%"=="b" goto min_start
if %sec_start% geq 0 (
    if %sec_start% leq 59 (
        if %sec_start% lss 10 set sec_start=0%sec_start%) else (
    echo Invalid second
    goto sec_start))

set /a hr_stop=23
set /p hr_stop=What hour to stop? 
if "%hr_stop%"=="b" goto sec_start
if %hr_stop% geq 0 (
    if %hr_stop% leq 23 (
        if %hr_stop% lss 10 set hr_stop=0%hr_stop%) else (
    echo Invalid hour
    goto hr_stop))

set min_stop=59
set /p min_stop=What minute to stop? 
if "%min_stop%"=="b" goto hr_stop
if %min_stop% geq 0 (
    if %min_stop% leq 59 (
        if %min_stop% lss 10 set min_stop=0%min_stop%) else (
    echo Invalid minute
    goto min_stop))

set /a sec_stop=59
set /p sec_stop=What second to stop? 
if "%sec_stop%"=="b" goto min_stop
if %sec_stop% geq 0 (
    if %sec_stop% leq 59 (
        if %sec_stop% lss 10 set sec_stop=0%sec_stop%) else (
    echo Invalid second
    goto sec_stop))

set start_disp=%hr_start%:%min_start%:%sec_start%
set stop_disp=%hr_stop%:%min_stop%:%sec_stop%
echo Start time is %start_disp%.
echo Stop time is %stop_disp%.

set start_hr=%hr_start%*10000
set /a start_min=%min_start%*100

set /a stop_hr=%hr_stop%*10000
set /a stop_min=%min_stop%*100

set /a start_time=%start_hr% + %start_min% + %sec_start%
set /a stop_time=%stop_hr% + %stop_min% + %sec_stop%

if %stop_time% leq %start_time% (
echo End time must be at least one second after start time
goto time_begin)

set stop_disp=%hr_stop:~-2%:%min_stop%:%sec_stop%
echo Start time is %start_disp%.
echo Stop time is %stop_disp%.
set corr_time=blank
set /p corr_time=Times are correct?
if /i "%corr_time%"=="y" goto summary
if /i "%corr_time%"=="n" goto time_begin
goto time_confirm

As you can see, I have two ways I could search: an integer using hour, minute, and second, or a string using %start_disp% and %stop_disp%. Here is a sample line, and all lines follow the same pattern exactly up to this point in the line; after the final ], the pattern varies:

2013/10/30 00:00:13  [501014]CODELINE_INDICATION_MSG  TX  41 bytes
share|improve this question
2013/10/30 00:04:27 [501005] ---> 20131030000427 [501005] if you transform the entries as above then they can be used as simple integers and you can calculate greater than or less than. Batch math is limited to 2GB values but this would work in a VBS script. –  foxidrive Nov 7 '13 at 7:07
I changed the sample line to one with the rest of the line to show the variable portion of it, as well. I also need the date and time formats to be as they are for other uses. Thanks for the suggestion! –  SyteCyer Nov 7 '13 at 7:21
Ok. The variable part is not an issue - but for ease of calculation you may prefer to rewrite and transform the date/time into a temporary file and then process that. –  foxidrive Nov 7 '13 at 7:33

1 Answer 1

solution with sed for Windows:

  • input file is sorted already:

    sed -n "\#START TIME#,\#STOP TIME#p" input.txt>output.txt
  • input file is not sorted:

    sort input.txt | sed -n "\#START TIME#,\#STOP TIME#p" >output.txt
  • example for a sorted file:

    sed -n "\#2013/10/30 00:00:13#,\#2013/11/30 00:00:13#p" input.txt>output.txt
share|improve this answer
since I will have to use this batch on virtually any computer, I would rather not have to install sed on them before running the search. If it were just my machine, this sounds like a great solution. –  SyteCyer Nov 9 '13 at 14:57

Your Answer


