Avatar of samiam41
samiam41Flag for United States of America asked on

Extract data from text file

Hey Experts!  I need to check a log file on several servers daily.  Rather than checking them everyday, I was hoping I could run a script and dump out the most recent data.  Here is a brief sample of the data:

03/04/2014 19:07:45 --- SCHEDULEREC QUERY BEGIN
03/04/2014 19:07:45 --- SCHEDULEREC QUERY END
03/04/2014 19:07:45 Next operation scheduled:
03/04/2014 19:07:45 ------------------------------------------------------------
03/04/2014 19:07:45 Schedule Name:         DAILYINC_7PM
03/04/2014 19:07:45 Action:                Incremental
03/04/2014 19:07:45 Objects:              
03/04/2014 19:07:45 Options:              
03/04/2014 19:07:45 Server Window Start:   19:00:00 on 03/05/2014
03/04/2014 19:07:45 ------------------------------------------------------------
03/04/2014 19:07:45 Schedule will be refreshed in 12 hours.
03/05/2014 07:07:45 TSM Backup-Archive Client Version 6, Release 4, Level 0.11  
03/05/2014 07:07:45 Querying server for next scheduled event.
03/05/2014 07:07:45 Node Name: ---
03/05/2014 07:07:45 Session established with server ---: Linux/x86_64
03/05/2014 07:07:45   Server Version 6, Release 3, Level 4.0
03/05/2014 07:07:45   Server date/time: 03/05/2014 07:07:45  Last access: 03/04/2014 19:07:45

03/05/2014 07:07:45 --- SCHEDULEREC QUERY BEGIN
03/05/2014 07:07:45 --- SCHEDULEREC QUERY END
03/05/2014 07:07:45 Next operation scheduled:
03/05/2014 07:07:45 ------------------------------------------------------------
03/05/2014 07:07:45 Schedule Name:         DAILYINC_7PM
03/05/2014 07:07:45 Action:                Incremental
03/05/2014 07:07:45 Objects:              
03/05/2014 07:07:45 Options:              
03/05/2014 07:07:45 Server Window Start:   19:00:00 on 03/05/2014
03/05/2014 07:07:45 ------------------------------------------------------------
03/05/2014 07:07:45 Schedule will be refreshed in 12 hours.
03/05/2014 19:07:45 TSM Backup-Archive Client Version 6, Release 4, Level 0.11  
03/05/2014 19:07:45 Querying server for next scheduled event.
03/05/2014 19:07:45 Node Name: ----
03/05/2014 19:07:45 Session established with server ---: Linux/x86_64
03/05/2014 19:07:45   Server Version 6, Release 3, Level 4.0
03/05/2014 19:07:45   Server date/time: 03/05/2014 19:07:45  Last access: 03/05/2014 09:04:24

I would like the script to look for data with a certain date, set by me manually.  I need to copy the data found that matches the date (example 3/05/2014) to one, central log file.

So experts, how does one do this?  :)

Thanks in advance.
Windows BatchWindows Server 2003Windows 7

Avatar of undefined
Last Comment
samiam41

8/22/2022 - Mon
becraig

Powershell
$value = "03/04/2014"
gc file.txt | select-string -pattern $value | out-file log-file.txt -append

Open in new window

#I added append if you were updating a log that you will continue to write to.
oBdA

Try something like this; you can pass the date either in the command line, or you'll be queried for it:
@echo off
setlocal
set LogFile=D:\Temp\test.log
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
type "%LogFile%" | findstr.exe "^%FindDate%" >>"%CentralLog%"
if errorlevel 1 (
	echo Date not found!
	exit /b 1
)

Open in new window

Of course, the log file could be collected from several servers as well, while we're at it. Is the log file in the same path on all machines, or does the path differ?
ASKER
samiam41

becraig, thank you!  checking into it now.

oBdA, good to see you again.  There are several servers that have this log file and the log file is either on the C, D or E partition (sorry).  

Thanks for the help!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
samiam41

oBdA, if I run this from my pc, how/where would I specify the hostname of the other servers?
SOLUTION
becraig

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Bill Prew

Here's a fairly simple approach if the names of the log files to be searched don't change day to day.  If you want to be prompted for the date then just use the logic for that already posted.
@echo off
setlocal

set CentralLog=Z:\temp\centrallog.txt
set LogFiles="C:\Temp\logfile1.txt" "D:\Temp\logfile1.txt" "E:\Temp\logfile1.txt"
set FindDate=01/01/2014

find "%FindDate%" %LogFiles% >> "%CentralLog%"

Open in new window

~bp
ASKER
samiam41

Hey BP!  Nice to see you again!

Can I run this script from my local pc and have it pull from a server?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
samiam41

@oBdA,

Here is the script I am using.  I only modified the entries below the DATA section.
@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

I get the prompts for date and then get this error:

C:\Tools\Scripts>dsm_copy.bat
Enter date to search: 03/07/2014
Processing '\\w2k3ps01\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log' ...
The system cannot find the path specified.

I tried using these in the DATA field and with two different servers (with and without the quotes).

"\\w2k3ps01\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log"
\\w2k3ps01\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log
ASKER
samiam41

@BP,

I figured out I could build a batch script for each server and have it run at x time.  As long as the servers all have the drive mapped to the same network share and use z:, they can copy the necessary info.  Does that mean I need to be logged into the servers for this script to work?
oBdA

You don't need to put quotes around the path when you enter it in the [DATA] section; the script will take care of that.
Did you change line 11 (which could actually be moved to line 3) to the actual location of your central log file?
What happens when you enter dir "\\w2k3ps01\d$\Program Files\Tivoli\TSM\baclient\dsmsched.log" in a command prompt?
If you're running the script from Windows 7, did you start it from an elevated command prompt?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
samiam41

@oBdA, I found the error and it was on my side.  I never changed the "Set CentralLog" variable and with no D: drive, the script failed.  I made that change and it worked.  I did add the dir and the script wasn't happy so I took it out and it went back to working.  

Thank you sir!  Great to work with you again.  I want to test out the BP solution as well as the other one before closing out and awarding points.  I will close out by the end of today regardless.
ASKER
samiam41

**I am posting a follow up question to this if anyone wants to jump in while it is still "fresh" on your mind**

https://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_28382929.html
ASKER
samiam41

I appreciate everyone's time and assistance with this.  I took oBdA's code and am building off of it as it was the easiest (for me) to use and understand (I'm still a rook with scripting).  Thank you all and I hope to work with you again!

-Aaron
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.