Solved

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

Posted on 2014-03-07
5
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 84

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 84

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OfficeMate Freezes on login or does not load after login credentials are input.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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…

732 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