从文件中提取一些数据,以便对它们进行特定处理(批量处理)

时间:2013-06-03 16:22:09

标签: batch-file findstr

我需要从文件中提取一些数据,以便将它们应用于特定的过程。文件结构如下所示

      some title information at file header

      I001=send login information to the system
       connection is OK            
      A001=System has answered: "you are connected"


      I002=press the log out button
      A002=system has answered: "You have been disconnected"
      disconnection is OK

      I003= timeout is 10
      If timeout occurs, refresh view and then check if connection is come again 
      Else retry refreshment until you are connected to the system 

我的目标是让I001,I002,I003,......只能在一条线上。 为此,我写了

    more +1 %1 | findstr /v /r "^A...=.*"   

但是我得到了

       I001=send login information to the system
       connection is OK
       I002=press the log out button
       I003= timeout is 10
       If timeout occurs, refresh view and then check if connection is come again. 
       Else retry refreshment until you are connected to the system

而不是

      I001=send login information to the system
      I002=press the log out button     
      I003= timeout is 10

另一方面,请让我知道如何通过管道立即将此结果传递给使用文件作为参数的vbs脚本。当我使用

         findstr "I[0-9][0-9][0-9]" "%~1"|cscript myscript.vbs, 

我在fsoObj.opentextfile(wscript.arguments(0),1).readall上遇到错误,fsoObj = createobject(“scripting.filesystemobject”)。

非常感谢您的帮助

3 个答案:

答案 0 :(得分:1)

如果您搜索I +三个号码,只需使用:

findstr "I[0-9][0-9][0-9]" "%~1"

..无论如何,在表达之前是什么,并且more也不需要。

答案 1 :(得分:0)

我不是100%确定我理解正确,但如果您只想要以I...=开头的行,那么您可以这样做:

type %1 | findstr /r "I...=.*"

根据您的输入,这将打印...

      I001=send login information to the system
      I002=press the log out button
      I003= timeout is 10

... 我认为就是你想要的。

答案 2 :(得分:0)

首先,摆脱/v选项,它与您想要的完全相反。

  

/ V仅打印不包含匹配项的行。

您还在开始时寻找A,但您想要I,因此请将其更改为

more +1 %1 | findstr /r "^I...=.*"

注意:上面的文件文本缩进了6个空格。在那种情况下使用

more +1 %1 | findstr /r "I...=.*"

(没有^,因为它只匹配开始的行与I,之前没有空格。)