批处理脚本以提取指定单词之间的行 [英] Batch Script To Extract Lines Between Specified Words

查看:114
本文介绍了批处理脚本以提取指定单词之间的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的日志文件.

I have a log file like below.

[2014年8月19日星期二10:45:28]本地/PLPLAN/PL/giuraja @ MSAD/2172/Info(1019025)

[Tue Aug 19 10:45:28 2014]Local/PLPLAN/PL/giuraja@MSAD/2172/Info(1019025)

从规则对象中读取数据库[PL]的规则

Reading Rules From Rule Object For Database [PL]

[2014年8月19日星期二10:45:28]本地/PLPLAN/PL/giuraja @ MSAD/2172/Info(1013157)

[Tue Aug 19 10:45:28 2014]Local/PLPLAN/PL/giuraja@MSAD/2172/Info(1013157)

使用[AIF0142.rul]和数据文件[SQL]从用户[giuraja @ MSAD]接收命令[导入]

Received Command [Import] from user [giuraja@MSAD] using [AIF0142.rul] with data file [SQL]

.

.

.

.

.

清除用户[giuraja @ MSAD]实例[1]上的活动

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

.

.

我想提取以"[[Tue Aug 19 10:]"开头的行,直到以"Clear Active on User"开头的行,并使用Windows批处理脚本输出到文件.我尝试了下面的代码.它仅输出最后一行.

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

@echo off & setlocal enabledelayedexpansion

设置Month_Num =%date:〜4,2%

set Month_Num=%date:~4,2%

如果%Month_Num%== 08设置为Month_Name = Aug

if %Month_Num%==08 set Month_Name=Aug

设置日期=%日期:〜0.3%

set Day=%date:~0,3%

设置Today_Date =%date:〜7.2%

set Today_Date=%date:~7,2%

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

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

对于/f"tokens = 1 delims = []" %% a in('find/n%Search_String%" ^

@(

for /f "tokens=1 delims=[]" %%a in ('find /n "%Search_String%"^

@(

更多+ %% a D:\ Hyperion \ ERPI_Actuals_Load \ Logs \ PLPLAN.LOG)> D:\ Hyperion \ ERPI_Actuals_Load \ Logs \ PLPLAN_Temp.txt

more +%%a D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN.LOG)>D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN_Temp.txt

(对于(D:\ Hyperion \ ERPI_Actuals_Load \ Logs \ PLPLAN_Temp.txt中的/f"tokens = *" %% a)执行(

(for /f "tokens=*" %%a in (D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN_Temp.txt) do (

设置测试= %% a

set test=%%a

如果!测试:〜0.20!" equ在用户上清除活动" goto:eof

if "!test:~0,20!" equ "Clear Active on User" goto :eof

回声%% a

))> D:\ Hyperion \ ERPI_Actuals_Load \ Logs \ PLPLAN_Formatted.txt

))>D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN_Formatted.txt

关于, 拉加夫.

推荐答案

下面的批处理文件旨在尽可能快地处理大文件.但是,它会从结果中删除空行:

The Batch file below was designed to process as fast as possible a big file; however, it deletes empty lines from result:

@echo off
setlocal EnableDelayedExpansion

set "start=[Tue Aug 19 10:"
set "end=Clear Active on User"

for /F %%a in ("%start%") do set startWord=%%a
for /F %%a in ("%end%") do set endWord=%%a

set "startLine="
set "endLine="
for /F "tokens=1,2 delims=: " %%a in ('findstr /I /N /B /C:"%start%" /C:"%end%" logFile.txt') do (
   if not defined startLine if "%%b" equ "%startWord%" set startLine=%%a
   if not defined endLine if "%%b" equ "%endWord%" set "endLine=%%a" & goto continue0
)
:continue0
set /A skipLines=startLine-1, numLines=endLine-startLine+1
set "skip="
if %skipLines% gtr 0 set skip=skip=%skipLines%

(for /F "%skip% delims=" %%a in (logFile.txt) do (
   echo %%a
   set /A numLines-=1
   if !numLines! equ 0 goto continue1
)) > outFile1.txt
:continue1

rem Previous outFile1.txt contain as many extra lines as empty lines removed, so we need to eliminate they

for /F "delims=:" %%a in ('findstr /I /N /B /C:"%end%" outFile1.txt') do set numLines=%%a
(for /F "delims=" %%a in (outFile1.txt) do (
   echo %%a
   set /A numLines-=1
   if !numLines! equ 0 goto continue2
)) > outFile2.txt
:continue2

del outFile1.txt
TYPE outFile2.txt

如果您要保留空行,则过程会慢很多[em] .

If you want to preserve empty lines, the process would be much slower.

这篇关于批处理脚本以提取指定单词之间的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆