gpt4 book ai didi

.net - Powershell io.streamreader “-context”等效命令

转载 作者:行者123 更新时间:2023-12-02 23:45:34 26 4
gpt4 key购买 nike

我在PowerShell中使用以下代码从大型txt文件收集相关数据。

$sr = new-object System.io.streamreader(get-item c:\test.txt)

while (($line = $sr.ReadLine()) -ne $null) {
if ($line -match "data") { $line }
}

$sr.Dispose()

这很好用,但是如果有与PowerShell的 select-string -context 1,0等效的功能,它能够使上面的行和匹配的行有效。

有人可以协助吗?

最佳答案

如果只需要前一行,则满足以下条件:

$sr = new-object System.io.streamreader(get-item c:\test.txt)

while (($line = $sr.ReadLine()) -ne $null) {
if ($line -match "data") { $prev_line, $line }
$prev_line = $line
}

$sr.Dispose()

关于.net - Powershell io.streamreader “-context”等效命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24001506/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com