Link to home
Start Free TrialLog in
Avatar of kunalap
kunalapFlag for India

asked on

batch file to parse a text file and pick out certain strings

hi.
i have a problem.
i actually have to write a batch file to generate a command to restore databases automatically.

one of the parameters in the command is giving a timestamp from the backup catalog file. backup catalog file is nothing but a text file which holds timestamps of when backups were taken.

now, the format of the catalog file is as follows:
-------------------------------------------------------------------------------------------------
begin dump=0
   mbt_version=5.0.00
   locale=en_US.iso88591
   datetime=12/25/2005 22:00:09
   completion_time=12/26/2005 00:09:13
   backup_expired=N
   file=adsm.exe:TSM_BACKUP_RCO_SRTSQL\backup_poolC:/BACKTRACK:RCO-SR~1\SRT\RKDB-25-12-2005.22:00:26-1428<43>
   pool=TSM_BACKUP_RCO_SRTSQL
   backuptype=data
   contents=no
   logicalheader=no
   compression=yes
   recovery_model=FULL
   encryption=no
   first_lsn=-3071208515949061311
   last_lsn=-3071208515948661311
   object_list=E:\Sqlbacktrack\catalog\mbacktrack\catalog\physical8\RCO-SRTSQL\SRT\RKDB.12_25_2005_22_00_09.lst
end dump
begin dump=log_only
   mbt_version=5.0.00
   locale=en_US.iso88591
   datetime=12/26/2005 00:15:01
   completion_time=12/26/2005 00:15:17
   backup_expired=N
   file=disk.exe:RKDB_TLOGD:\SQLBAC~1\RKDB_T~1\RKDB-26-12-2005_00.15.12.483-4096<1>
   pool=RKDB_TLOG
   backuptype=data
   contents=no
   logicalheader=no
   compression=yes
   recovery_model=FULL
   encryption=no
   first_lsn=-3072208515930661311
   last_lsn=-3071208515948161311
end dump
-------------------------------------------------------------------------------------------------

This loops on and on. I need only the "datetime=...." entry for every "dump=0" section. maybe even where "backup_expired=Y" but backup_expired is not really important.

so what i need help with is....something to which i can give this catalog file as the input...and all the "datetime=...." lines will be printed of screen where "dump=0"

please let me know if im not explaining correctly.

thanks in advance.
much appreciated.

-kunal
Avatar of GuruGary
GuruGary
Flag of United States of America image

Hi, Kunalap.

I think I figured out a solution.  I'm not sure this is the best way to do this, so if one of the other experts comes up with a better way, feel free to give them the points.  I think that you want to search a file (given as a parameter) for lines ending in "dump=0" and when found, print (to the screen) the first occurance of the line containing "datetime".  I am making the following assumptions:
 - This batch file will be run from a writable directory under Windows 2000/XP/2003
 - This batch file accepts 1 parameter which is the log file to parse
 - The "datetime" line will always be 3 lines down from the "dump=0" line
 - The length of the "datetime" line is constant (31 characters)

I think the following should work:

@echo off
setlocal
set file=%1

if {%1}=={} echo Please specify filename&goto :EOF
if not exist %1 echo Can't find file %file%&goto :EOF

for /f %%a in ('findstr /i /n "dump=0$" %file%') do set dumpline=%%a&call :Process
goto :EOF

:Process
set /a start=2+%dumpline% 2>NUL
more /e  +%start% %file% > temptxt.txt
echo rcx >debugcmd.txt&echo 21 >>debugcmd.txt&echo w >>debugcmd.txt& echo q >>debugcmd.txt
debug temptxt.txt <debugcmd.txt >NUL
type temptxt.txt
del temptxt.txt
del debugcmd.txt

goto :EOF


ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Kunalap - I would use Steve's solution because it is cleaner than mine, has the capability for additional processing and has less dependancies than my solution (good job, Steve!)
Thanks Gary :)
Avatar of kunalap

ASKER

god bless u man.
damn u guys are good.

cheers.

thanks.