需要获取Select-String macch的行号

问题描述 投票:0回答:1

真的需要你的帮助。

我有一个 $combobox,它为我提供了所有包含“错误来源”的文件名。

<$ComboBox.Items.AddRange(@(Get-ChildItem $filepath -Recurse -Include ".xml",".txt"| 选择字符串“错误来源”-List | 选择对象文件名))>

我需要获取文件中发生匹配的行号。

我一直在寻找,但没有任何作用。你能帮忙吗?

powershell line-numbers select-string
1个回答
0
投票

要在 XML 和 TXT 文件中搜索字符串“错误源”并使用找到匹配项的文件名和行号填充组合框,应执行以下操作:

# Iterate over XML and TXT files in the specified directory and its subdirectories
Get-ChildItem $filepath -Recurse -Include ".xml", ".txt" |

# Search for the string "wrong source" in the files; retrieve only the first match from each file
Select-String "wrong source" -List |

# For each match found, create a custom object with the filename and the line number
ForEach-Object {
    [PSCustomObject]@{
        FileName   = $_.FileName;
        LineNumber = $_.LineNumber;
    }
} |

# Add the custom objects to the ComboBox
$ComboBox.Items.AddRange(@($_))
© www.soinside.com 2019 - 2024. All rights reserved.