如何跳过两个连续的匹配线

时间:2015-12-14 08:44:11

标签: batch-file text command-line skip

我的目标是从不必要的行清除日志文件,这是我的第一个每日统计脚本:

set CDPath=C:\ConnectDirect
set CDWin="C:\CDwin\Common Utilities\Direct"
echo sel stats startt=(8:30:00) stopt=(22:30:00) recids | %CDWin% -x > %CDPath%\report.log

这是一份报告:

==============================================================================
                                Select Statistics
==============================================================================
P RECID  LOG TIME            PNAME     PNUMBER STEPNAME CCOD FDBK   MSGID
E RECID  LOG TIME            MESSAGE TEXT
------------------------------------------------------------------------------
E SSTR   12/12/2015 12:00:37 SNODE session started - remote node target                                                        
P PSTR   12/12/2015 12:00:37 ##SNT$1N  22024            0    0      LSMG200I
P PRED   12/12/2015 12:00:37 ##SNT$1N  22024            0    0      SCPA000I
E SSTR   12/12/2015 21:37:12 SNODE session started - remote node target                                                        
P PSTR   12/12/2015 21:37:12 ##SNT$1N  23109            0    0      LSMG200I
P PRED   12/12/2015 21:37:12 ##SNT$1N  23109            0    0      SCPA000I
E SSTR   12/12/2015 21:37:14 SNODE session started - remote node target                                                        
P PSTR   12/12/2015 21:37:14 ##SNT$1N  23110            0    0      LSMG200I
P PRED   12/12/2015 21:37:15 ##SNT$1N  23110            0    0      SCPA000I
E SSTR   12/12/2015 21:37:16 SNODE session started - remote node target                                                        
P PSTR   12/12/2015 21:37:16 ##SNT$1N  23111            0    0      LSMG200I
P PRED   12/12/2015 21:37:19 ##SNT$1N  23111            0    0      SCPA000I
E SSTR   12/12/2015 21:37:19 SNODE session started - remote node target                                                        
P PSTR   12/12/2015 21:37:19 ##SNT$1N  23112            0    0      LSMG200I
P PRIN   12/12/2015 21:37:21           0                8    0      LSMI008I
E SSTR   12/12/2015 21:37:21 PNODE session not started - remote node 00000000                                                  
P PRIN   12/12/2015 21:37:23           0                8    0      LSMI008I
E SSTR   12/12/2015 21:37:23 PNODE session not started - remote node 00000000                                                  
P PRED   12/12/2015 21:37:24 ##SNT$1N  23112            0    0      SCPA000I
E SSTR   12/12/2015 21:37:24 SNODE session started - remote node target                                                        
P PSTR   12/12/2015 21:37:24 ##SNT$1N  23114            0    0      LSMG200I
P PRED   12/12/2015 21:37:24 ##SNT$1N  23114            0    0      SCPA000I
E SSTR   12/12/2015 21:40:23 SNODE session started - remote node target                                                        
P PSTR   12/12/2015 21:40:23 ##SNT$1N  23113            0    0      LSMG200I
P PRED   12/12/2015 21:40:25 ##SNT$1N  23113            0    0      SCPA000I

现在我想创建一个新的文件,删除所有符合此模式的行!“!/ PNODE会话未启动/并跳过下一个近线

E SSTR   12/12/2015 21:37:21 PNODE session not started - remote node 00000000                                                  
P PRIN   12/12/2015 21:37:23           0                8    0      LSMI008I

我尝试将此脚本与awk一起使用,但仅适用于第一行但不适用于第二行

gawk  "!/PNODE session not started/ {print; next}" Report.log > newreport.log

第二行也必须准确记录此记录

  0                8    0      LSMI008I

如何删除第二行? 提前致谢

3 个答案:

答案 0 :(得分:0)

@ECHO OFF
SETLOCAL
SET "skip2="
(
FOR /f "delims=" %%a IN (q34262715.txt) DO (
 ECHO %%a|FINDSTR /l /c:"PNODE session not started" /c:"INODE session not started" >NUL
 IF ERRORLEVEL 1 (
  IF NOT DEFINED skip2 ECHO(%%a
  SET "skip2="
 ) ELSE (SET skip2=y)
)
)>u:\newfile.txt

GOTO :EOF

我使用了一个名为q34262715.txt的文件,其中包含我的测试数据。

制作u:\ newfile.txt

  • 将每行数据读入%%a
  • 检查该行是否包含任一目标字符串
  • 如果是,请设置skip2
  • 如果没有,那么如果设置了skip2,则前一行匹配,请跳过此并清除skip2
  • 否则,重现行

答案 1 :(得分:0)

此任务的另一个批处理解决方案比Magoo编写的代码更快,因为没有使用findstr非常慢。

@echo off
setlocal EnableDelayedExpansion
set "Skip=0"
(
    for /F "usebackq eol=ÿ delims=" %%L in ("%CDPath%\report.log") do (
        if "!Skip!" == "1" (
            set "Skip=0"
        ) else (
            set "Line=%%L"
            if "!Line:PNODE session not started=!" == "!Line!" (
                echo %%L
            ) else (
                set "Skip=1"
            )
        )
    )
) >"%CDPath%\FilteredReport.log"
endlocal

从报告文件中读取每个非空行。

跳过变量Skip没有值0的所有行。

由于将值1赋值给变量PNODE session not started,因此也会跳过包含Skip不区分大小写的行以及下一行。

所有其他行都通过命令 echo 输出,此输出将重定向到与FilteredReport.log相同的目录中的文件report.log

eol=ÿ只需确保命令 FOR 在读取报告文件中的行时不会跳过以分号开头的行。

输入数据示例的结果是:

==============================================================================
                                Select Statistics
==============================================================================
P RECID  LOG TIME            PNAME     PNUMBER STEPNAME CCOD FDBK   MSGID
E RECID  LOG TIME            MESSAGE TEXT
------------------------------------------------------------------------------
E SSTR   12/12/2015 12:00:37 SNODE session started - remote node target                                                        
P PSTR   12/12/2015 12:00:37 ##SNT$1N  22024            0    0      LSMG200I
P PRED   12/12/2015 12:00:37 ##SNT$1N  22024            0    0      SCPA000I
E SSTR   12/12/2015 21:37:12 SNODE session started - remote node target                                                        
P PSTR   12/12/2015 21:37:12 ##SNT$1N  23109            0    0      LSMG200I
P PRED   12/12/2015 21:37:12 ##SNT$1N  23109            0    0      SCPA000I
E SSTR   12/12/2015 21:37:14 SNODE session started - remote node target                                                        
P PSTR   12/12/2015 21:37:14 ##SNT$1N  23110            0    0      LSMG200I
P PRED   12/12/2015 21:37:15 ##SNT$1N  23110            0    0      SCPA000I
E SSTR   12/12/2015 21:37:16 SNODE session started - remote node target                                                        
P PSTR   12/12/2015 21:37:16 ##SNT$1N  23111            0    0      LSMG200I
P PRED   12/12/2015 21:37:19 ##SNT$1N  23111            0    0      SCPA000I
E SSTR   12/12/2015 21:37:19 SNODE session started - remote node target                                                        
P PSTR   12/12/2015 21:37:19 ##SNT$1N  23112            0    0      LSMG200I
P PRIN   12/12/2015 21:37:21           0                8    0      LSMI008I
E SSTR   12/12/2015 21:37:24 SNODE session started - remote node target                                                        
P PSTR   12/12/2015 21:37:24 ##SNT$1N  23114            0    0      LSMG200I
P PRED   12/12/2015 21:37:24 ##SNT$1N  23114            0    0      SCPA000I
E SSTR   12/12/2015 21:40:23 SNODE session started - remote node target                                                        
P PSTR   12/12/2015 21:40:23 ##SNT$1N  23113            0    0      LSMG200I
P PRED   12/12/2015 21:40:25 ##SNT$1N  23113            0    0      SCPA000I

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • set /?
  • setlocal /?
  • echo /?

答案 2 :(得分:0)

我写了这个,似乎工作正常

@echo off
setlocal EnableDelayedExpansion
set "Skip=0"
(
    for /F "usebackq eol= delims=" %%L in ("D:\report_inp.log") do (
        if "!Skip!" == "1" (
            set "Skip=0"
        ) else (
            set "Line=%%L"
            if "!Line:          0                8    0      LSMI008I=!" == "!Line!" (
                    if "!Line:PNODE session not started - remote node=!" == "!Line!" (
                echo %%L
                )
            ) else (
                set "Skip=1"
            )
        )
    )
) >"D:\Report_Out.log"
endlocal

问题解决了,感谢大家的支持!