You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本功能扩展需求:特定字符串匹配后关联验证ID与目标字符串的同行存在性

Alright, let's build out your PowerShell script to cover all those extended requirements. Here's a complete, commented solution that does exactly what you need, plus some extra debugging help:

Complete PowerShell Script
# Find the first occurrence of "6,16" in any .txt file, grab up to 50 lines after it (adjust the 50 if needed)
$initialMatch = Select-String -Path ".\*.txt" -Pattern "6,16" -Context 0, 50 | Select-Object -First 1

if ($initialMatch) {
    # Look through the lines after the match to find the one with "ID number:"
    $idLine = $initialMatch.Context.PostContext | Where-Object { $_ -match 'ID number:(.*)' }
    
    if ($idLine) {
        # Extract the ID number (trim any extra spaces just in case)
        $targetID = $matches[1].Trim()
        Write-Host "Extracted ID: $targetID"

        # Check export.txt to see if this ID is on the same line as "6,16"
        $exportValidation = Select-String -Path ".\export.txt" -Pattern "^.*$targetID.*6,16.*$"
        
        if ($exportValidation) {
            Write-Host "✅ Success! ID $targetID is paired with '6,16' in export.txt:`n$($exportValidation.Line)"
        } else {
            Write-Warning "⚠️ ID $targetID doesn't appear with '6,16' in export.txt. Let's check further:"
            # Optional: Check if the ID exists at all in export.txt
            $idOnlyCheck = Select-String -Path ".\export.txt" -Pattern $targetID
            if ($idOnlyCheck) {
                Write-Host "ID found in export.txt (but not with '6,16') on these lines:`n$($idOnlyCheck.Line -join "`n")"
            } else {
                Write-Host "ID $targetID is not present in export.txt at all."
            }
        }
    } else {
        Write-Warning "⚠️ No line with 'ID number:' found after the first '6,16' match."
    }
} else {
    Write-Warning "⚠️ No '6,16' matches found in any .txt files in the current directory."
}
Breaking Down the Key Parts
  • Context Buffer: I swapped your original -Context 20 for -Context 0,50—this means we only care about lines after the "6,16" match, and we grab up to 50 of them. Adjust the 50 to a smaller number if you know the ID line is always close by, or larger if it might be further away.
  • ID Extraction: The regex 'ID number:(.*)' captures everything to the right of "ID number:", and Trim() cleans up any accidental leading/trailing spaces in the ID.
  • Validation Check: The regex ^.*$targetID.*6,16.*$ ensures the ID and "6,16" are on the same line, regardless of their order. If you need them in a specific sequence (like "6,16" comes first), tweak the regex to ^.*6,16.*$targetID.*$.
  • Debugging Help: The script includes fallback checks to tell you if the ID exists in export.txt without the "6,16" match, or if it's missing entirely—this saves you from guessing what went wrong.
Quick Customization Tips
  • If the "ID number:" line is always immediately after the "6,16" match, replace the Where-Object line with $idLine = $initialMatch.Context.PostContext[0] for a faster lookup.
  • To process all "6,16" matches instead of just the first, remove the | Select-Object -First 1 and wrap the logic in a foreach ($match in $initialMatch) { ... } loop.

内容的提问来源于stack exchange,提问作者Sv3n

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 03:02:27