kunalap
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_R CO_SRTSQL\ backup_poo lC:/BACKTR ACK:RCO-SR ~1\SRT\RKD B-25-12-20 05.22:00:2 6-1428<43>
pool=TSM_BACKUP_RCO_SRTSQL
backuptype=data
contents=no
logicalheader=no
compression=yes
recovery_model=FULL
encryption=no
first_lsn=-307120851594906 1311
last_lsn=-3071208515948661 311
object_list=E:\Sqlbacktrac k\catalog\ mbacktrack \catalog\p hysical8\R CO-SRTSQL\ SRT\RKDB.1 2_25_2005_ 22_00_09.l st
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\R KDB_T~1\RK DB-26-12-2 005_00.15. 12.483-409 6<1>
pool=RKDB_TLOG
backuptype=data
contents=no
logicalheader=no
compression=yes
recovery_model=FULL
encryption=no
first_lsn=-307220851593066 1311
last_lsn=-3071208515948161 311
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
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
backup_expired=N
file=adsm.exe:TSM_BACKUP_R
pool=TSM_BACKUP_RCO_SRTSQL
backuptype=data
contents=no
logicalheader=no
compression=yes
recovery_model=FULL
encryption=no
first_lsn=-307120851594906
last_lsn=-3071208515948661
object_list=E:\Sqlbacktrac
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
backup_expired=N
file=disk.exe:RKDB_TLOGD:\
pool=RKDB_TLOG
backuptype=data
contents=no
logicalheader=no
compression=yes
recovery_model=FULL
encryption=no
first_lsn=-307220851593066
last_lsn=-3071208515948161
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 :)
ASKER
god bless u man.
damn u guys are good.
cheers.
thanks.
damn u guys are good.
cheers.
thanks.
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