Solved Batch script to extract lines between specified words

August 19, 2014 at 11:13:07
Specs: Windows server 2008 R2
Hi,

I have a log file like below.

.
.
.
[Tue Aug 19 05:01:33 2014]Local/PLPLAN///11480/Info(1013214)
Clear Active on User [Automation@Native Directory] Instance [1]
.
.
[Tue Aug 19 10:45:28 2014]Local/PLPLAN/PL/giuraja@MSAD/2172/Info(1019025)
Reading Rules From Rule Object For Database [PL]

[Tue Aug 19 10:45:28 2014]Local/PLPLAN/PL/giuraja@MSAD/2172/Info(1013157)
Received Command [Import] from user [giuraja@MSAD] using [AIF0142.rul] with data file [SQL]

.
.
.
.
.
Clear Active on User [giuraja@MSAD] Instance [1]
.
.


I want to extract the line starting with "[Tue Aug 19 10:" until the line that starts with "Clear Active on User" and output to a file using windows batch script. I tried the below code. It only outputs the last line.

@echo off & setlocal enabledelayedexpansion

set Month_Num=%date:~4,2%

if %Month_Num%==08 set Month_Name=Aug

set Day=%date:~0,3%

set Today_Date=%date:~7,2%

set Search_String=[%Day% %Month_Name% %Today_Date% 10:

for /f "tokens=1 delims=[]" %%a in ('find /n "%Search_String%"^<D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN.LOG') do

@(
more +%%a D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN.LOG)>D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN_Temp.txt
(for /f "tokens=*" %%a in (D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN_Temp.txt) do (
set test=%%a
if "!test:~0,20!" equ "Clear Active on User" goto :eof
echo %%a
))>D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN_Formatted.txt
::========= end batchscript

message edited by ragavhere


See More: Batch script to extract lines between specified words

Report •


#1
August 19, 2014 at 20:13:13
✔ Best Answer
I boiled it down some, to make it easier to analyze. This worked with my test-file:

@echo off & setlocal enabledelayedexpansion
set eo=Clear Active on User [giuraja@MSAD] Instance [1]
set eo=%eo:~0,20%

:: substitute your formatted date string for the following
set Search_String=three

for /f "tokens=1 delims=[]" %%a in ('find /n "%Search_String%"^<ragav.LOG') do (
more +%%a ragav.log>ragav.tmp
)
echo ragav.tmp:
type ragav.tmp
pause

(for /f "tokens=*" %%a in (ragav.tmp) do (
set test=%%a
if "!test:~0,20!" equ "%eo%" goto :eof
echo %%a
))>ragav.xxx
::========= end batchscript



Report •

#2
September 16, 2014 at 09:53:35
Hi,

Thanks. Just got time to test this.

I tried this script and doesn't seem to be working. It outputs the lines that is after "Clear Active on User [Automation@Native Directory] Instance [1]". But not between search_String and eo.

@echo off & setlocal enabledelayedexpansion
set eo=Clear Active on User [Automation@Native Directory] Instance [1]
set eo=%eo:~0,20%

:: substitute your formatted date string for the following
set Search_String=[Mon Sep 15 21:

for /f "tokens=1 delims=[]" %%a in ('find /n "%Search_String%"^<PLPLAN.LOG') do (
more +%%a PLPLAN.LOG>PLPLAN.tmp
)
echo PLPLAN.tmp:
type PLPLAN.tmp
pause

(for /f "tokens=*" %%a in (PLPLAN.tmp) do (
set test=%%a
if "!test:~0,20!" equ "%eo%" goto :eof
echo %%a
))>PLPLAN.txt
::========= end batchscript

Regards,
Ragav.


Report •

#3
September 16, 2014 at 19:01:48
I re-tested on my end and everything worked. You might need to post or pmail your data, but my test-file worked. Did the .tmp file look right? (1 line past search-string, thru eof). I should have not put the screen display - it is confusing. That is NOT the output. The output will be in the file PLPlan.txt.

Report •

Related Solutions


Ask Question