Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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
Powershell 选择要查找的字符串,然后从找到的单词中选择字符串的一部分3行_Powershell - Fatal编程技术网

Powershell 选择要查找的字符串,然后从找到的单词中选择字符串的一部分3行

Powershell 选择要查找的字符串,然后从找到的单词中选择字符串的一部分3行,powershell,Powershell,我正在尝试使用powershell执行以下操作: 我试图搜索一个单词,一旦找到这个单词,我需要排3行,读34到42个字符,然后打印出来 测试文件: 我要在这里走很长一段路。我们沿着河边走 雅达雅达 拉拉——她说 滚,滚,他说 井干了 我所拥有的: 选择字符串$TestFile-pattern“well”-simplematch 3{34,42} 我不明白的是: 如何打印“步行”部分 每次打印出整行1时都会发生这种情况。2.三,。 我只需要一部分“走路” 如果有帮助的话,我会把它用于日志文件。在文

我正在尝试使用powershell执行以下操作: 我试图搜索一个单词,一旦找到这个单词,我需要排3行,读34到42个字符,然后打印出来

测试文件:

  • 我要在这里走很长一段路。我们沿着河边走 雅达雅达
  • 拉拉——她说
  • 滚,滚,他说
  • 井干了
  • 我所拥有的: 选择字符串$TestFile-pattern“well”-simplematch 3{34,42}

    我不明白的是: 如何打印“步行”部分

    每次打印出整行1时都会发生这种情况。2.三,。 我只需要一部分“走路”


    如果有帮助的话,我会把它用于日志文件。在文件中,“错误”一词在错误代码下面三行,通常是一个字符串。

    使用上下文参数:

    PS> (Get-ChildItem $TestFile | Select-String well -Context 3).Context.PreContext[-3].Substring(35,15)
    walk walk walk
    

    为什么不使用select string的-context参数呢?我可以得到-context 3来打印第三行,但是如何在该字符串中选择一个单词呢?3{35,15}不会更改值输出。
    PS>(gc C:\temp\sls.txt | sls“test”-Context 3)。Context | select-ExpandProperty pretext | select-first 1
    sls是select字符串的别名(仅在powershell V3中可用)Shay,不管怎样,你可以把它作为一个脚本而不是使用管道。我将字符串设置为一个文本文件,然后将它设置为一个变量。尝试执行$blah2=($text select string-path textfile.txt-pattern“well”-context.context.pretotext[-3].Substring(35,15)当我尝试打印$blah2时出现错误抱歉,粘贴命令时出错,请在上下文参数后添加“3”。请参阅更新的命令。在prectext[此处的计数是多少,并且相关行是否不为空?(get ChildItem$TestFile | Select String well-Context 3)。Context.prectext.count