Solved

Copy text between two fields in a log file to a central log file

Posted on 2014-03-07
5
332 Views
Last Modified: 2014-03-13
Hi Experts!  With the help of a few brilliant minds, I have script that will query a log file on several servers and copy the data in that log file based on the date field to a central log file.

The next step for this script is to copy the data that starts with
03/06/2014 19:04:08 --- SCHEDULEREC STATUS BEGIN
and ends with
03/06/2014 19:04:08 --- SCHEDULEREC STATUS END

In this example, I would use 03/06/2014 as the date the script needs to look for in the log file, find --- SCHEDULEREC STATUS BEGIN and copy that and all text below it until it reaches this line --- SCHEDULEREC STATUS END

I am attaching the script that is already built to find the date in the log file so that we can build off of it.  Thank you experts!!

@echo off
setlocal
if "%~1"=="" (
	set FindDate=
	set /p FindDate=Enter date to search: 
) else (
	set FindDate=%~1
)
if "%FindDate%"=="" goto :eof
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /i /n "[DATA]"') do set DataStart=%%a
set CentralLog=D:\Temp\test_Central.log
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
	echo Processing '%%a' ...
	type "%%a" | findstr.exe "^%FindDate%" >>"%CentralLog%"
	if errorlevel 1 (
		echo Date not found in '%%a'.
	)
)
GOTO :EOF

[DATA]
\\w2k3ps01\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log

Open in new window

0
Comment
Question by:samiam41
  • 3
  • 2
5 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 39913381
Try this:
@echo off
setlocal enabledelayedexpansion
set CentralLog=D:\Temp\test_Central.log
if "%~1"=="" (
	set FindDate=
	set /p FindDate=Enter date to search: 
) else (
	set FindDate=%~1
)
if "%FindDate%"=="" goto :eof
set CopyBegin=SCHEDULEREC STATUS BEGIN
set CopyEnd=SCHEDULEREC STATUS END
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /i /n "[DATA]"') do set DataStart=%%a
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
	echo Processing '%%a' ...
	set /a CopyFound = 0
	set /a Copy = 0
	for /f "delims=" %%b in ('type "%%a" ^| findstr.exe "^%FindDate%"') do (
		set Line=%%b
		if !Copy!==0 (
			if not "!Line!"=="!Line:%CopyBegin%=!" (
				set /a CopyFound = 1
				set /a Copy = 1
				>>"%CentralLog%" echo !Line!
			)
		) else (
			>>"%CentralLog%" echo !Line!
			if not "!Line!"=="!Line:%CopyEnd%=!" (
				set /a Copy = 0
			)
		)
	)
	if !CopyFound!==0 (
		echo No match found in '%%a'.
	)
)
goto :eof

[DATA]
\\w2k3ps01\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log

Open in new window

0
 
LVL 9

Author Comment

by:samiam41
ID: 39926237
Hi oBdA!  Here is where this script is and the error I'm getting.

First, this is the script I'm using:
@echo off
setlocal enabledelayedexpansion
set CentralLog=Z:\Logs\TSM_Backups\Combined_TSMLogs.log
if "%~1"=="" (
	set FindDate=
	set /p FindDate=Enter date to search: 
) else (
	set FindDate=%~1
)
if "%FindDate%"=="" goto :eof
set CopyBegin=SCHEDULEREC STATUS BEGIN
set CopyEnd=SCHEDULEREC STATUS END
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /i /n "[DATA]"') do set DataStart=%%a
for /f "skip=%DataStart% delims=" %%a in ('type "%~f0"') do (
	echo Processing '%%a' ...
	set /a CopyFound = 0
	set /a Copy = 0
	for /f "delims=" %%b in ('type "%%a" ^| findstr.exe "^%FindDate%"') do (
		set Line=%%b
		if !Copy!==0 (
			if not "!Line!"=="!Line:%CopyBegin%=!" (
				set /a CopyFound = 1
				set /a Copy = 1
				>>"%CentralLog%" echo !Line!
			)
		) else (
			>>"%CentralLog%" echo !Line!
			if not "!Line!"=="!Line:%CopyEnd%=!" (
				set /a Copy = 0
			)
		)
	)
	if !CopyFound!==0 (
		echo No match found in '%%a'.
	)
)
goto :eof

[DATA]
\\w2k3ps01\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log

Open in new window


This is the log file that the script should be pulling the data from:

03/11/2014 01:24:42 --- SCHEDULEREC STATUS BEGIN
03/11/2014 01:24:42 Total number of objects inspected:      180,837
03/11/2014 01:24:42 Total number of objects backed up:          182
03/11/2014 01:24:42 Total number of objects updated:              0
03/11/2014 01:24:42 Total number of objects rebound:              0
03/11/2014 01:24:42 Total number of objects deleted:              0
03/11/2014 01:24:42 Total number of objects expired:              0
03/11/2014 01:24:42 Total number of objects failed:               0
03/11/2014 01:24:42 Total number of subfile objects:              0
03/11/2014 01:24:42 Total number of bytes inspected:          46.32 GB
03/11/2014 01:24:42 Total number of bytes transferred:        14.20 GB
03/11/2014 01:24:42 Data transfer time:                   15,794.72 sec
03/11/2014 01:24:42 Network data transfer rate:              943.07 KB/sec
03/11/2014 01:24:42 Aggregate data transfer rate:            659.29 KB/sec
03/11/2014 01:24:42 Objects compressed by:                       19%
03/11/2014 01:24:42 Total data reduction ratio:               69.34%
03/11/2014 01:24:42 Subfile objects reduced by:                   0%
03/11/2014 01:24:42 Elapsed processing time:               06:16:33
03/11/2014 01:24:42 --- SCHEDULEREC STATUS END
03/11/2014 01:24:42 --- SCHEDULEREC OBJECT END DAILYINC_7PM 03/10/2014 19:00:00
03/11/2014 01:24:42 Scheduled event 'DAILYINC_7PM' completed successfully.
03/11/2014 01:24:42 Sending results for scheduled event 'DAILYINC_7PM'.
03/11/2014 01:24:42 Results sent to server for scheduled event 'DAILYINC_7PM'.

I'm getting this error when I run the script from a command prompt:

C:\Tools\Scripts>backup_master_log.bat
Enter date to search: 03/11/14
Processing '\\w2k3ps01\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log' ...
No match found in '\\w2k3ps01\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log'
.

Not sure what the problem is.  I confirmed the dsmsched.log file is in the directory specified in the script.  Help.  :)
0
 
LVL 83

Assisted Solution

by:oBdA
oBdA earned 500 total points
ID: 39926806
Try to search for "03/11/2014" instead of "03/11/14".
0
 
LVL 9

Author Closing Comment

by:samiam41
ID: 39926908
Brilliant work!!  Thanks oBdA.  I will have one more follow-up question to this and would appreciate any time you can spend on it.

Take care!

-Aaron
0
 
LVL 9

Author Comment

by:samiam41
ID: 39926956
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now