具有更多字符串的文本中的Findstr

时间:2014-01-24 23:35:22

标签: batch-file cmd

我的文件格式如下:

Station Number : 1 , IP: 10.1.1.10 
01:28:39.540  ..........  ..........  ....................  ....................  Task: Office & Ej Hot Backup was finished with status: Successful

Station Number : 3 , IP: 10.1.3.10
01:35:32.217  ..........  ..........  ....................  ....................  Task: Office & Ej Hot Backup was finished with status: Successful

Station Number : 4 , IP: 10.1.4.10 
01:25:33.382  ..........  ..........  ....................  ....................  Task: Office & Ej Hot Backup was finished with status: Successful

Station Number : 6 , IP: 10.1.6.10 
01:23:16.958  ..........  ..........  ....................  ....................  Task: Office & Ej Hot Backup was finished with status: Successful

我想创建一个从中收集数据的文件,如下所示:

Station Number : X,Task: Office & Ej Hot Backup was finished with status: Successful

我已经尝试过使用findstr并且被卡住了,感谢他们的帮助!

26.1.2014编辑:::: 大家好!谢谢你的帮助和帮助快速回复! 我做了这个:

:: day sets ::
set mm=%date:~-10,2%
set dd=%date:~-7,2%
set yyyy=%date:~-4%
set TODAY=%dd%-%mm%-%yyyy%
set filenotfound=********************* %%A , File Not Found *********************
set Computerslist=IPST.TXT
set LOG=C:\MaintSRV\%today%_MaintSRV_All_Stations.TXT

for /f "tokens=1,2" %%A in (%Computerslist%) do (
    cls
    @echo Collecting MaintSrv Status From Station Number : %%A
    for /f "tokens=*" %%F in ('findstr /c:"was finished with status" \\%%B.10\C\office\services\log\MAINTSRV-%yyyy%%mm%%dd%.LOG') DO @echo St %%A   %%F>>%log%
    IF EXIST "\\%%B.10\C\office\services\log\MAINTSRV-%yyyy%%mm%%dd%.LOG" (echo complete) ELSE (echo %filenotfound%>>%log%) 
    )

它的效果很好,但我得到了一些我想要删除的点(........)。 这是输出:

St 1   01:28:55.329  ..........  ..........  ....................  ....................  Task: Office & Ej Hot Backup was finished with status: Successful
St 3   01:36:32.913  ..........  ..........  ....................  ....................  Task: Office & Ej Hot Backup was finished with status: Successful
St 4   01:26:00.125  ..........  ..........  ....................  ....................  Task: Office & Ej Hot Backup was finished with status: Successful
St 6   01:23:33.416  ..........  ..........  ....................  ....................  Task: Office & Ej Hot Backup was finished with status: Successful

这是想要的输出:

St 1   01:28:55.329  Task: Office & Ej Hot Backup was finished with status: Successful
St 3   01:36:32.913  Task: Office & Ej Hot Backup was finished with status: Successful
St 4   01:26:00.125  Task: Office & Ej Hot Backup was finished with status: Successful
St 6   01:23:33.416  Task: Office & Ej Hot Backup was finished with status: Successful

3 个答案:

答案 0 :(得分:0)

以下是嵌套for循环的一个例子:

@echo off

setlocal ENABLEDELAYEDEXPANSION

for /f "delims=" %%i in (some.log) do (
  for /f "tokens=1,2 delims=:," %%j in ("%%i") do (
    if "%%j"=="Station Number " (
      set _num=%%k
      set _num=!_num: =!
    ) else (
      set _task=%%i
      set _task=!_task:~82!
      echo Station Number : !_num!,!_task!>>new.log
    )
  )
)

endlocal

鉴于您的输入文件是some.log,以下是运行批处理文件后new.log文件的样子。

Station Number : 1,Task: Office & Ej Hot Backup was finished with status: Successful
Station Number : 3,Task: Office & Ej Hot Backup was finished with status: Successful
Station Number : 4,Task: Office & Ej Hot Backup was finished with status: Successful
Station Number : 6,Task: Office & Ej Hot Backup was finished with status: Successful

答案 1 :(得分:0)

编辑以适应更改后的问题中所要求的新格式

这使用名为repl.bat的帮助程序批处理文件 - 从https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

下载

repl.bat放在与批处理文件相同的文件夹中或放在路径上的文件夹中。

@echo off
type "file.txt" |repl ".*r : (\d+).*\r\n(.*?) .*(Task.*)" "St $1   $2   $3" xm >"newfile.txt"

答案 2 :(得分:0)

以下批处理文件生成输出,如下所述“这是想要的输出”:

@echo off
setlocal EnableDelayedExpansion

set "St="
(for /F "tokens=1-5*" %%A in (input.txt) do (
   if not defined St (
      set St=%%D
   ) else (
      echo St !St!   %%A  %%F
      set "St="
   )
)) > output.txt

input.txt中提供输入文件,这是生成的output.txt

St 1   01:28:39.540  Task: Office & Ej Hot Backup was finished with status: Successful
St 3   01:35:32.217  Task: Office & Ej Hot Backup was finished with status: Successful
St 4   01:25:33.382  Task: Office & Ej Hot Backup was finished with status: Successful
St 6   01:23:16.958  Task: Office & Ej Hot Backup was finished with status: Successful