powershell - 从多个文本文件的大型数据集中收集唯一项目
问题描述
我正在使用 PowerShell 从多个文本文件中收集名称列表。这些文件中的名称可能相似/重复。我试图确保 PowerShell 返回一个包含所有唯一项目的文本文件。在查看数据时,脚本似乎正在收集 271/296 个独特项目。我猜有些数据不应该被标记为重复,有什么建议吗?
#Take content of each file (all names) and add unique values to text file
#for each unique value, create a row & check to see which txt files contain
function List {
$nofiles = Read-Host "How many files are we pulling from?"
$data = @()
for ($i = 0;$i -lt $nofiles; $i++)
{
$data += Read-Host "Give me the file name for file # $($i+1)"
}
return $data
}
function Aggregate ($array) {
Get-Content $array | Sort-Object -unique | Out-File newaggregate.txt
}
#SCRIPT BODY
$data = List
aggregate ($data)
我期待这段代码能够捕捉到所有内容,但它缺少一些看起来非常相似的项目。缺失名称及其类似匹配的列表:
CORPINZUTL16 MISSING FROM OUTFILE
CORPINZTRACE MISSING FROM OUTFILE
CORPINZADMIN Found In File
我有大约 20 个这样的例子。显然 Get-Content -Unique 并没有检查一行中的每个字符。谁能推荐一种更好的方法来检查每一行或可能强制获取字符检查全名?
解决方案
只是为了演示,这一行创建了 3 个带有数字的 txt 文件
for($i=1;$i -lt 4;$i++){set-content -path "$i.txt" -value ($i..$($i+7))}
1.txt | 2.txt | 3.txt | newaggregate.txt
1 | | | 1
2 | 2 | | 2
3 | 3 | 3 | 3
4 | 4 | 4 | 4
5 | 5 | 5 | 5
6 | 6 | 6 | 6
7 | 7 | 7 | 7
8 | 8 | 8 | 8
| 9 | 9 | 9
| | 10 | 10
在这里使用Get-Content
一系列[1-3]
文件
Get-Content [1-3].txt | Sort-Object {[int]$_} -Unique | Out-File newaggregate.txt
$All = Get-Content .\newaggregate.txt
foreach ($file in (Get-ChildItem [1-3].txt)){
Compare-Object $All (Get-Content $file.FullName) |
Select-Object @{n='File';e={$File}},
@{n="Missing";e={$_.InputObject}} -ExcludeProperty SideIndicator
}
File Missing
---- -------
Q:\Test\2019\05\07\1.txt 9
Q:\Test\2019\05\07\1.txt 10
Q:\Test\2019\05\07\2.txt 1
Q:\Test\2019\05\07\2.txt 10
Q:\Test\2019\05\07\3.txt 1
Q:\Test\2019\05\07\3.txt 2
推荐阅读
- java - 空值而不是 LinkedHashmap 中的值
- python - 气流将长时间运行的任务标记为失败
- node.js - 如何从firebase云功能向我的应用引擎django应用程序发出请求承诺请求
- c++ - SChannel 中的密码套件选择
- java - 对数据结构中的特定元素进行排名 - 是一种更有效的方法吗?
- javascript - 如何在heroku上全局安装npm包?
- c# - 需要帮助使用 C# 从 CSV 文件中删除特定的命名列
- flutter - Flutter:限制 webview_flutter 长按手势或打开链接预览
- python - 如何将 sns.facetgrid 保存为 pdf
- javascript - 如何通过单击另一个字段来获取 id 的输入