如何使用批处理脚本

时间:2016-08-01 09:21:25

标签: batch-file

我有一个将在多个服务器上执行的命令列表。有两个特定的命令从系统获取日志,我需要解析这两个日志文件,只获取所需的数据,目前正在使用以下代码。但是我无法弄清楚如何检查执行的命令是否是那两个日志命令的命令,如果是,继续执行其余的逻辑,如果没有,则跳过接下来的几行。我无法找到如何在批处理脚本中使用if语句,也可以使用OR条件。

虽然我现在使用for循环,但是我只能检查一个命令但不能同时检查两个命令

命令列表文件内容: IPCONFIG SYS1 / LOG1 FDISK MAP1 / LOG1

从上面的命令,我想检查命令中的任何一个,即sys1 / log1或map1 / log1是否执行低于逻辑

代码:

版本:1.0

    for /f %%i in ('findstr /li /c:"sys1/log1" /c:"ipconfig" !MyLog!') do (
    set "wanted=WARNING MINOR MAJOR"
    echo echo severity  date        time    Description > !MyLog!_out.log
     (
    for /f "tokens=1,2 delims==" %%a in ('type !MyLog!^|findstr "severity= date= time= description="') do (
      if "%%a"=="    severity" echo\
    REM this is four SPACES above before severity
      <nul set /p "=%%b   "
     REM above there is a SPACE and a TAB after %%b 
     )
    echo/
     )  >>!MyLog!_out.log
     type !MyLog!_out.log | findstr /b "severity %wanted%" > !MyLog!_out.log
 )

版本:1.1

1       for /f %%i in ('findstr /li /c:"sys1/log1" /c:"ipconfig" !MyLog!') do 2(
3       set "wanted=WARNING MINOR MAJOR"
4       echo echo severity  date        time    Description > !MyLog!_out.log
5        (
6        for /f "tokens=1,2 delims==" %%a in ('type !MyLog!^|findstr "severity= date= time= description="') do (
7          if "%%a"=="    severity" echo\
8       REM this is four SPACES above before severity
9          <nul set /p "=%%b   "
10         REM above there is a SPACE and a TAB after %%b 
11       )
12        echo/
13       )  >>!MyLog!_out.log
14   for %%a in (!MyLog!) do (
15    echo %%a | find /n /i !wanted! >> !MyLog!_out.log
16    echo %%a
17  )
18     )

请提出任何想法/建议/帮助。

MyLog.log输出:

echo severity  date        time    Description 

MINOR 01/04/2015   08:56   Maintenance note: App  logs cleared through sys tool
WARNING 02/22/2015   19:59   POST Error: Caching will be enabled once Super-Cap has been charged. No action is required.   
WARNING 03/13/2015   14:34   POST Error:  Caching will be enabled once Super-Cap has been charged. No action is required.   
MAJOR 10/13/2015   15:03   Network Adapter Link Down (Slot 0, Port 1)   
MAJOR 01/13/2016   13:03   Network Adapter Link Down (Slot 0, Port 0)   
MAJOR 01/21/2016   02:39   Network Adapter Link Down (Slot 0, Port 0)   
MAJOR 06/09/2016   01:01   Network Adapter Link Down (Slot 0, Port 0)

1 个答案:

答案 0 :(得分:0)

一旦您实现了 rojo 的建议,请在外部FOR循环中合并此脚本而不是您的内部循环,并在此处发布任何问题:

@echo off
setlocal enabledelayedexpansion
-------------------------------------

rem Inside your outer FOR loop, add this construct

set "wanted=WARNING MINOR MAJOR"
for %%a in ("%wanted%") do (
    for %%b in (!MyLog!) do (
        echo %%b | find /n /i %%a >nul >> !MyLog!_out.log
        echo %%a %%b))