【问题标题】:How can I add data, insert rows and delete columns in a CSV file using powershell and then save it?如何使用 powershell 在 CSV 文件中添加数据、插入行和删除列,然后保存?
【发布时间】:2017-03-01 23:00:07
【问题描述】:

我有一个包含大量列和大量数据的大型 CSV 文件,现在我要做的就是删除其中的一些列,并在剩余列的最顶部插入一行并提供新的列标题。

我拥有并希望将其导入 PowerShell 的 CSV 文件示例:

Server A    January  C:drive     Medium  Security  Logged
Server A    March    D:drive     High    Security  Logged
Server B    March    D:drive     High    Audit     Logged
Server B    May      F:drive     Low     Audit     Logged
Server C    May      E:drive     Low     Audit     Logged

我想使用 PowerShell/脚本来获得这样的输出:

Month    Drive       Alert
January  C:drive     Medium
March    D:drive     High
March    D:drive     High
May      F:drive     Low
May      E:drive     Low

所以基本上又一次, 1)删除不需要的列。 2)在最顶部插入一个新行。 3) 将新数据添加到新行。 4) 最后导出 CSV 文件。

感谢大家的帮助:)

【问题讨论】:

  • 只需在电子表格编辑器中手动完成 :-)

标签: powershell csv import export-to-csv


【解决方案1】:

试试这个

    $fileContent=import-csv C:\temp\ddddd.csv -Delimiter ";" -Header Server, Month, Drive, Alert, security, Log | select Month, Drive, Alert 
    $newRow = New-Object PsObject -Property @{ Month = 'December' ; Drive = 'X:drive' ; Alert='Low'}
    $fileContent += $newRow
    $fileContent | export-csv C:\temp\ddddd2.csv -NoType

【讨论】:

  • 嘿,谢谢,但是您知道如何使用 powershell 删除整列或一行吗?谢谢
猜你喜欢
  • 2021-11-11
  • 2021-02-19
  • 2019-04-30
  • 2018-12-29
  • 2015-09-28
  • 2023-03-30
  • 1970-01-01
  • 2015-02-08
  • 1970-01-01
相关资源
最近更新 更多