Solved

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

Posted on 2014-03-07
5
337 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 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

Are your AD admin tools letting you down?

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

Suggested Solutions

Title # Comments Views Activity
Cannot take ownership of a folder 8 44
SOA*.tmp files 2 44
SRV.SYS Causing Server Crash During Backup 11 31
How to do SFTP in Windows to another server. 3 17
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…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

861 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