【发布时间】:2019-07-21 00:56:46
【问题描述】:
从一份相当复杂的合同中,我有两个 .txt 文件。文件如下所示:
文件1:
Hash: 5FD876CDF0FCFAF1E7F018F5A8519A7B
Path: /Users/foobar/Desktop/whatever.jpg
文件2:
Hash: 0EDEFB152D489163E603F8C55F5463A7
Path: c:\Migration\Templates\script.exe
这里的目标是,比较 File1 和 File2 并找到匹配的哈希。找到匹配的哈希后,将它们(以及以下路径)输出到另一个 .txt 文件中。
我在谷歌上搜索了太多问题,但大多数解决方案都是为了找出差异或设计不同,我在 Powershell 方面的专业知识还不足以正确地重写它们。
# pattern to match the Hashes from File2
$pattern = "^[a-f0-9]{32}$"
# read in File1 as an array to compare to
$set1 = Get-Content -Path 'C:\File1.txt'
# prepare to collect the results
$results = New-Object -TypeName System.Text.StringBuilder
# start scanning the text2 file for matches to text1 entries
Get-Content -Path 'C:\File2.txt'
if ($_ -match $pattern)
{
$hashes = $Matches['hash']
if ($hashes -in $set1)
{
[void]$results.AppendLine($_)
}
}
}
# output the results
$results.ToString() | Out-File -FilePath 'C:\output.txt' -Encoding ascii
上面的代码还不够匹配,我需要帮助来完成最后的润色!
感谢您阅读我的帖子!
【问题讨论】:
-
"上面的代码还不够匹配" - 请包括 1) 你期望发生的事情,2) 实际发生的事情,以及 3) 修复语法不正确/不完整的代码 sn-p
标签: powershell