powershell - 从多个文本文件的大型数据集中收集唯一项

标签 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不会检查一行中的每个字符。任何人都可以推荐一种更好的方法来检查每一行,或者可能强制get-字符检查全名吗?

最佳答案

只是为了演示,此行创建了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

关于powershell - 从多个文本文件的大型数据集中收集唯一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56028382/

相关文章:

将服务器重启划分为每周一次的算法

powershell - 如何使用 Powershell 对目录中的所有文件执行程序

powershell - Powershell与管道中的可枚举

arrays - 在本地子网中查找可用设备名称并重命名设备

sql-server - 需要帮助通过 PowerShell 模块 "PSExcel"将工作表添加到现有 xlsx

powershell - 如何管理处理从另一个对象扩展的字符串列表?

xml - 如何在 PowerShell 中编写 Xml.linq?

java - Powershell 作为 unix 系统上的 java 进程,cmdlet 不显示输出

c# - 在 PowerShell 中调用通用静态方法

windows - 当前两个字符为 ZZ 时删除文件