1

嘿,我希望对一些数据进行重复数据删除并合并 CSV 中的列。我不知道该怎么做。这是我正在使用的数据示例:

cmmc,stig,descr
AC.1.001,SV-205663r569188_rule,The ability to set access permissions and auditing is critical to maintaining the security and proper access controls of a system. To support this volumes must be formatted using a file system that supports NTFS attributes.
AC.1.001,SV-205667r569188_rule,Inappropriate granting of user rights can provide system administrative and other high-level capabilities.
AC.1.002,SV-205663r569188_rule,The ability to set access permissions and auditing is critical to maintaining the security and proper access controls of a system. To support this volumes must be formatted using a file system that supports NTFS attributes.
AC.1.002,SV-205665r569188_rule,Enterprise Domain Controllers groups on domain controllers.

我非常接近我正在寻找的数据,但努力|<value of 'descr'>在第二列中的项目之后添加:

这是我的脚本:

Import-CSV '.\input.csv' | Group-Object 'cmmc' |
    ForEach-Object {
        [PsCustomObject]@{
            cmmc = $_.name
            stig = $_.group.stig -Join '
'
                    }
    } | Export-Csv '.\output.csv' -NoTypeInformation

输出看起来像这样(为便于阅读而格式化,省略了列名):

AC1.001    SV-205663r569188_rule
           SV-205665r569188_rule
AC1.002    SV-205663r569188_rule
           SV-205665r569188_rule

但我正在寻找这个:

AC.1.001 SV-205663r569188_rule|The ability to set access permissions and auditing is critical to maintaining the security and proper access controls of a system. To support this volumes must be formatted using a file system that supports NTFS attributes.
         SV-205667r569188_rule|Inappropriate granting of user rights can provide system administrative and other high-level capabilities.
AC.1.002 SV-205663r569188_rule|The ability to set access permissions and auditing is critical to maintaining the security and proper access controls of a system. To support this volumes must be formatted using a file system that supports NTFS attributes.
         SV-205665r569188_rule|Enterprise Domain Controllers groups on domain controllers.
4

1 回答 1

1

使用以下内容,它将计算的属性Select-Object与应用于Group-Object调用结果的 cmdlet 结合使用:

Import-Csv .\input.csv | 
  Group-Object cmmc |
    Select-Object @{ Name = 'cmmc'; e = 'Name' },
      @{ Name = 'stig_descr'; e = { 
          [array] $stigs, [array] $descrs, $i = $_.Group.stig, $_.Group.descr, 0
          $sigs.ForEach( { $stigs[$i], $descrs[$i++] -join '|' }) -join "`n" 
        } 
      } | Export-Csv -NoTypeInformation -Encoding utf8 .\output.csv

注意:
•和的[array]类型约束用于处理组仅包含一条记录的情况,在这种情况下,由于成员枚举的行为, and仅返回单个字符串rater 而不是单元素数组;如果没有强制转换,那么将对实例执行索引(例如) ,这将从字符串中返回该位置的单个字符。 • 在通话中,根据需要进行调整。BOM-less UTF-8 现在是PowerShell (Core) 7+中的默认值,并且不再需要。$stigs$descrs$_.Group.sig$_.Group.descr[array][$i][string]
Export-Csv-Encoding-NoTypeInformation

生成的文件具有以下内容,显示了列内部换行符的使用(受包含在 中的整个值的保护"..."):

"cmmc","stig_descr"
"AC.1.001","SV-205663r569188_rule|The ability to set access permissions and auditing is critical to maintaining the security and proper access controls of a system. To support this volumes must be formatted using a file system that supports NTFS attributes.
SV-205667r569188_rule|Inappropriate granting of user rights can provide system administrative and other high-level capabilities."
"AC.1.002","SV-205663r569188_rule|The ability to set access permissions and auditing is critical to maintaining the security and proper access controls of a system. To support this volumes must be formatted using a file system that supports NTFS attributes.
SV-205665r569188_rule|Enterprise Domain Controllers groups on domain controllers."

为了可视化这会产生所需的数据,您可以重新导入生成的文件并Format-Table使用-Wrap开关将其通过管道传输:

PS> Import-Csv .\output.csv | Format-Table -Wrap

cmmc     stig_descr
----     ---------
AC.1.001 SV-205663r569188_rule|The ability to set access permissions and auditing is critical to maintaining the security and proper access controls of a system. To support this volumes must be formatted using a file system that supports NTFS attributes.
         SV-205667r569188_rule|Inappropriate granting of user rights can provide system administrative and other high-level capabilities.
AC.1.002 SV-205663r569188_rule|The ability to set access permissions and auditing is critical to maintaining the security and proper access controls of a system. To support this volumes must be formatted using a file system that supports NTFS attributes.
         SV-205665r569188_rule|Enterprise Domain Controllers groups on domain controllers.

请注意,这-Wrap尊重属性内部的换行符,但如果它们对于控制台窗口来说太宽,则另外将单独的行分成多个行。

于 2021-03-12T14:34:17.837 回答