Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 在批处理或VBS中搜索后获取上下文行_Batch File_Vbscript_Powershell - Fatal编程技术网

Batch file 在批处理或VBS中搜索后获取上下文行

Batch file 在批处理或VBS中搜索后获取上下文行,batch-file,vbscript,powershell,Batch File,Vbscript,Powershell,我有一个文本文件,用这种格式填写 Identifier: NUMBER1 Information1 = something Information2 = other something Information3 = more something Indentified: NUMBER2 Information1 = something Information2 = other something Information3 = more somet

我有一个文本文件,用这种格式填写

Identifier: NUMBER1
    Information1 = something
    Information2 = other something
    Information3 = more something

Indentified: NUMBER2
    Information1 = something
    Information2 = other something
    Information3 = more something
我尝试过使用findstr,但与grep不同的是,它无法提供上下文

如何获取上下文行以便搜索其他信息位

如果可以的话,我会为Windows安装grep和awk等工具,但这是一个供其他人使用的工具,因此我只能使用操作系统本身的工具,在本例中是批处理和VBS

我想做的等效unix表达式如下所示:

grep -A 4 "NUMBER1" file | grep "Information2" | awk ' { print $3 } ' #not sure if $3 or $2 off the top of my head
Get-Content <filename> | Where-Object {$_ -like "*<pattern>*"}
试试这个:

@ECHO off&SETLOCAL
对于('findstr/n“NUMBER1”文件)中的/f“delims=:”%%a,请设置“skip=%%a”
对于('more+%skip%file^ | findstr“Information2”')中的/f“tokens=2*”%%b,如果未定义行集,请执行“line=%%c”
回音%行%
另请参见
findstr/?

尝试以下操作:

@ECHO off&SETLOCAL
对于('findstr/n“NUMBER1”文件)中的/f“delims=:”%%a,请设置“skip=%%a”
对于('more+%skip%file^ | findstr“Information2”')中的/f“tokens=2*”%%b,如果未定义行集,请执行“line=%%c”
回音%行%

另请参见
findstr/?

除非您使用的是相当旧的系统,否则可能已安装Windows。使用Powershell,您可以执行类似于grep的操作,如下所示:

grep -A 4 "NUMBER1" file | grep "Information2" | awk ' { print $3 } ' #not sure if $3 or $2 off the top of my head
Get-Content <filename> | Where-Object {$_ -like "*<pattern>*"}
Get Content | Where对象{$\类似于“**”}

除非您使用的是相当旧的系统,否则您可能已经安装了Windows。使用Powershell,您可以执行类似于grep的操作,如下所示:

grep -A 4 "NUMBER1" file | grep "Information2" | awk ' { print $3 } ' #not sure if $3 or $2 off the top of my head
Get-Content <filename> | Where-Object {$_ -like "*<pattern>*"}
Get Content | Where对象{$\类似于“**”}

您可以在VBScript中执行以下操作:

If WScript.Arguments.Count <> 2 Then
  WScript.Echo "Usage: " & WScript.ScriptName & " pattern filename"
  WScript.Quit 1
End If

Const Context = 3 'lines

Set fso = CreateObject("Scripting.FileSystemObject")

Set re = New RegExp
re.Pattern = WScript.Arguments(0)

n = 0
Set f = fso.OpenTextFile(WScript.Arguments(1))
Do Until f.AtEndOfStream
  line = f.ReadLine
  If re.Test(line) Then
    WScript.Echo line
    n = Context
  ElseIf n > 0 Then
    WScript.Echo line
    n = n - 1
  End If
Loop
f.Close
如果WScript.Arguments.Count为2,则
Echo“用法:&WScript.ScriptName&“模式文件名”
WScript.Quit 1
如果结束
常量上下文=3'行
设置fso=CreateObject(“Scripting.FileSystemObject”)
Set re=New RegExp
re.Pattern=WScript.Arguments(0)
n=0
Set f=fso.OpenTextFile(WScript.Arguments(1))
直到f.AtEndOfStream
line=f.ReadLine
如果重新测试(线路),则
回音线
n=上下文
如果n>0,则
回音线
n=n-1
如果结束
环
f、 接近

无论如何,这只是一个非常基本的草稿,还有很大的改进余地(例如,将上下文行的数量作为参数,在上下文之前添加,等等)。

您可以在VBScript中执行以下操作:

If WScript.Arguments.Count <> 2 Then
  WScript.Echo "Usage: " & WScript.ScriptName & " pattern filename"
  WScript.Quit 1
End If

Const Context = 3 'lines

Set fso = CreateObject("Scripting.FileSystemObject")

Set re = New RegExp
re.Pattern = WScript.Arguments(0)

n = 0
Set f = fso.OpenTextFile(WScript.Arguments(1))
Do Until f.AtEndOfStream
  line = f.ReadLine
  If re.Test(line) Then
    WScript.Echo line
    n = Context
  ElseIf n > 0 Then
    WScript.Echo line
    n = n - 1
  End If
Loop
f.Close
如果WScript.Arguments.Count为2,则
Echo“用法:&WScript.ScriptName&“模式文件名”
WScript.Quit 1
如果结束
常量上下文=3'行
设置fso=CreateObject(“Scripting.FileSystemObject”)
Set re=New RegExp
re.Pattern=WScript.Arguments(0)
n=0
Set f=fso.OpenTextFile(WScript.Arguments(1))
直到f.AtEndOfStream
line=f.ReadLine
如果重新测试(线路),则
回音线
n=上下文
如果n>0,则
回音线
n=n-1
如果结束
环
f、 接近

无论如何,这只是一个非常基本的草稿,还有很大的改进余地(例如,将上下文行的数量作为参数,在上下文之前添加,等等)。

您可以使用一个非常有用的类似grep的工具,它是一个名为findrepl.bat的助手批处理文件,来自-


您还可以从搜索结果窗口进一步搜索。

您可以使用一个非常有用的类似grep的工具,它是一个名为findrepl.bat的助手批处理文件,来自-



您可以从搜索结果窗口进一步搜索。

请参阅我的编辑。我知道findstr做什么。它没有为我提供上下文行来执行额外的findstr。不知道,
为我提供上下文行应该是什么。我的代码已经提供了一行。@endro“Context”表示匹配行之前或之后的行。@endro不知道这些行的内容。@AnsgarWiechers我离开工作,将模式放入OP的变量中。请参阅我的编辑。我知道findstr做什么。它没有为我提供上下文行来执行额外的findstr。不知道,
为我提供上下文行应该是什么。我的代码已经提供了一行。@endro“Context”是指匹配行之前或之后的行。@endro不知道这些行的内容。@AnsgarWiechers我离职是为了在OP的变量中放入模式。我已经问过powershell(相信我),因为我们不能100%保证客户会有它,我不能用它。否则我就只需要选择字符串了-context@ardentsonata几年前,我会同意的。如今,Windows用户似乎不太可能没有powershell,因为自2009年以来,powershell已作为操作系统的一个组成部分发布。如果你与专业或公司客户打交道,这一点尤其正确。基本上,如果你正在处理一个不到五年的Win7系统,它将安装powershell。我并不反对。但这不是我的上级想要增加的要求,所以我不能使用它。“我坚持对每个人使用最低公分母,以确保完全兼容。”ardentsonata明白。祝你的项目好运。我已经问过powershell(相信我,我已经问过了),因为我们不能100%保证客户会拥有它,所以我不能使用它。否则我就只需要选择字符串了-context@ardentsonata几年前,我会同意的。如今,Windows用户似乎不太可能没有powershell,因为自2009年以来,powershell已作为操作系统的一个组成部分发布。如果你与专业或公司客户打交道,这一点尤其正确。基本上,如果你正在处理一个不到五年的Win7系统,它将安装powershell。我并不反对。但这不是我的上级想要增加的要求,所以我不能使用它。“我坚持对每个人使用最低公分母,以确保完全兼容。”ardentsonata明白。祝你的项目好运。这非常有效!非常感谢你。我是一个完全的VBSNUB(只知道sh和python)。我会编辑这个,使它更适合我的需要,但它正是我一直在寻找!这很好用!谢谢你