?
Solved

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

Posted on 2014-03-07
5
Medium Priority
?
349 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 86

Accepted Solution

by:
oBdA earned 2000 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 86

Assisted Solution

by:oBdA
oBdA earned 2000 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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
Suggested Courses

589 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