Solved

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

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

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 85

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

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…
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

728 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