【问题标题】:Compare 2 CSV files and write all differences比较 2 个 CSV 文件并写入所有差异
【发布时间】:2016-07-13 06:13:16
【问题描述】:

我有 3 个包含用户信息的 CSV 文件。 CSV1 是所有非活动用户的“主”列表。 CSV2 是当前需要停用的用户列表,CSV3 是需要激活的用户列表。

我想要的是有一个 PowerShell 脚本,可以从另一个脚本(创建 CSV2/3 的那个)调用它来比较 CSV1/2 并将所有唯一记录写回 CSV1。然后我希望它比较 CSV1/3 并删除 CSV1 中存在于 CSV3 中的所有记录。 CSV2/3 可以每天更改,其中可能没有数据,除了标题。

有几个独特的字段,但我想比较“EmployeeID”。 所有 3 个 CSV 文件都有标头(所有的标头都相同,因此数据是一致的)。

到目前为止,我最终将记录从 CSV2 添加到 CSV1,但它添加了两个标题。

$ICM= Import-Csv inactiveicmaster.csv -Header 'StudentDistrictID', 'StudentSiteCode', 'StudentLastName', 'StudentFirstName', 'StudentGradeLevel', 'GraduationYr', 'Masterck', 'Homeroom', 'MiddleName', 'Birthday', 'Gender', 'Email'
$IC = Import-Csv csv\inactiveic.csv -Header 'StudentDistrictID', 'StudentSiteCode', 'StudentLastName', 'StudentFirstName', 'StudentGradeLevel', 'GraduationYr', 'Masterck', 'Homeroom', 'MiddleName', 'Birthday', 'Gender', 'Email'
$DIS = Import-Csv csv\disinad.csv -Header 'StudentDistrictID', 'StudentSiteCode', 'StudentLastName', 'StudentFirstName', 'StudentGradeLevel', 'GraduationYr', 'Masterck', 'Homeroom', 'MiddleName', 'Birthday', 'Gender', 'Email'
foreach ($f in $ic) {
  $found = $false
  foreach ($g in $icm) {
    if ($g.StudentDistrictID -eq $f.StudentDistrictID) {
      $found = $true
    }
  }
  if ($found -eq $false) {
    $icm += $f
    if ($f.masterck -eq "") {
      $f.masterck = "IM"
    }
  }
}
<#
foreach ($h in $dis) {
  $found = $false
  foreach ($g in $icm) {
    if ($g.studentdistrictid -eq $h.studentdistrictid) {
      $found = $true
    }
    if ($found -ne $false) {
      #don't know what to do here to remove the duplicate
    }
  }
}
#>
$icm | select * | Export-Csv master.csv -NoTypeInformation

【问题讨论】:

  • 要开始使用,请查看 Compare-Object cmdlet。
  • 如果我从导入中删除 -Header,它只会在导出中添加一个。

标签: csv powershell


【解决方案1】:

我不知道确切的答案,但你不能这样做吗?

$file1 = import-csv -Path "C:\temp\Test1.csv" 
$file2 = import-csv -Path "C:\temp\Test2.csv" 
Compare-Object $file1 $file2 -property MPFriendlyName

查看此链接以获取完整示例和结果:Compare csv with same headers

如果您知道这些差异,则很容易将它们写入其他 csv。

编辑: 我对比较对象没有太多经验,但由于它是一个 csv,你可以用这个删除列。

Import-Csv C:\fso\csv1.csv | select ColumnYouWant1,ColumnYouWant2| Export-Csv -Path c:\fso\csvResult.csv –NoTypeInformation

此命令将读取您的最后一个 csv 并选择您要保留的列并将其导出到新的 csv。

添加一个 remote-item 命令以删除您不再需要的任何 csv 文件并完成。

【讨论】:

  • 使用 -passthru 解决这个问题,我能够删除在 CSV1/3 中重复的数据。但是,它会将 SideIndicator 添加到 csv 文件中。我怎么会去不添加呢?这是代码...$ms = import-csv master.csv compare-object $ms $dis -property studentdistrictid -passthru|export-csv inactiveicMastertest.csv -NoTypeInformation
【解决方案2】:

解决方案:

$ICM= Import-Csv InactiveICMaster.csv
$IC = Import-Csv csv\InactiveIC.csv
$DIS = Import-Csv csv\DisinAD.csv
foreach ($f in $ic)
{
    $found = $false
    foreach($g in $icm)
    {
        if ($g.StudentDistrictID -eq $f.StudentDistrictID) 
        {
            $found = $true
        }
    }
    if ($found -eq $false)
    {
        $icm += $f
        if ($f.masterck -eq "")
        {
            $f.masterck = "IM"
        }
    }
}
$icm | select * | export-csv InactiveICMaster.csv -NoTypeInformation
$icma = import-csv InactiveICMaster.csv
compare-object $icma $dis -property studentdistrictid -passthru|Where-Object {$_.SideIndicator -eq "<="}|select StudentDistrictID,StudentSiteCode,StudentLastName,StudentFirstName,StudentGradeLevel,GraduationYr,Masterck,Homeroom,MiddleName,Birthday,Gender,Email |export-csv inactiveicmastertest.csv -NoTypeInformation
remove-item inactiveicmaster.csv
import-csv inactiveicmastertest.csv|sort StudentDistrictID|export-csv InactiveICMaster.csv -NoTypeInformation
remove-item InactiveICMasterTest.csv

【讨论】:

    【解决方案3】:

    我知道这是旧的,但想为其他寻找此解决方案的人回答。我正在尝试自己使用 Compare-Object,因为这两个矩阵但是遇到了一个问题,如果一个比另一个大,它会永远运行一个非常大的矩阵,其中包含很多重复项。

    对于上述解决方案,您可能希望在为此目的嵌套循环时考虑使用中断。它可以让您更快地进行比较。 Break 将告诉第二个 for-each 循环停止并继续下一个项目。

    抱歉,第一次在这里发帖。不知道如何正确格式化,我得回去行动了。

    $ICM= Import-Csv InactiveICMaster.csv
    $IC = Import-Csv csv\InactiveIC.csv
    $DIS = Import-Csv csv\DisinAD.csv
    foreach ($f in $ic)
      foreach($g in $icm){
        if ($g.StudentDistrictID -eq $f.StudentDistrictID){
          break
        }else{
          $icm += $f
          if ($f.masterck -eq ""){
            $f.masterck = "IM"
          }
    
      }
    }
    $icm | select * | export-csv InactiveICMaster.csv -NoTypeInformation
    $icma = import-csv InactiveICMaster.csv
    compare-object $icma $dis -property studentdistrictid -passthru|Where-Object {$_.SideIndicator -eq "<="}|select StudentDistrictID,StudentSiteCode,StudentLastName,StudentFirstName,StudentGradeLevel,GraduationYr,Masterck,Homeroom,MiddleName,Birthday,Gender,Email |export-csv inactiveicmastertest.csv -NoTypeInformation
    remove-item inactiveicmaster.csv
    import-csv inactiveicmastertest.csv|sort StudentDistrictID|export-csv InactiveICMaster.csv -NoTypeInformation
    remove-item InactiveICMasterTest.csv
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 2016-09-05
      • 2018-11-19
      相关资源
      最近更新 更多