简体   繁体   English

如何使用Powershell在CSV文件中添加数据,插入行和删除列,然后保存?

[英]How can I add data, insert rows and delete columns in a CSV file using powershell and then save it?

I have a big CSV file with lots of columns and lots of data, now all I am looking to do is delete few of those columns and also insert a row at the very top for the remaining columns and give new column headings. 我有一个很大的CSV文件,其中包含许多列和大量数据,现在我要做的就是删除其中一些列,并在其余列的最上方插入一行,并给出新的列标题。

Example of the CSV file I have and would like to import it in PowerShell: 我拥有的CSV文件示例,想将其导入PowerShell中:

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

And I want to use PowerShell/script to get an output like this: 我想使用PowerShell /脚本获取如下输出:

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

So basically again, 1) removing unwanted columns. 所以基本上还是这样,1)删除不需要的列。 2) inserting a new row, at the very top. 2)在最上方插入新行。 3) adding new data to the new row. 3)将新数据添加到新行。 4) finally exporting the CSV file. 4)最后导出CSV文件。

Thanks for the help guys :) 谢谢你们的帮助:)

try this 尝试这个

    $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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Powershell从csv文件更新Active Directory中的更多数据行? - How can I update more data rows in Active Directory from a csv file using Powershell? 使用 Powershell,我如何导出和删除 csv 行,其中特定值*未在 *不同* csv 中找到*? - Using Powershell, how can I export and delete csv rows, where a particular value is *not found* in a *different* csv? 如何使用 Powershell 合并多个 CSV 文件,其中两列具有相同的数据 - How can i merge multiple CSV files in which two columns have same data using Powershell 在PowerShell中将行添加到CSV文件 - Add rows to CSV File in powershell 使用Powershell删除csv文件末尾的空行 - delete empty rows at end of the csv file using Powershell 如何使用Powershell脚本清理csv文件中的某些数据并将结果另存为新的csv文件? - How to clean up some data in a csv file using Powershell scripting and save the result as a new csv file? 如何使用 PowerShell 在我的 csv 文件中添加字符串并创建新列 - How can I add string and create new column in my csv file using PowerShell 如何使用PowerShell在CSV文件之间插入空白行? - How to insert empty rows in between in a CSV file with PowerShell? 如何将数据添加到现有 excel.csv 文件的行中? excel vba - how can i add data to rows of an existing excel.csv file? excel vba 如何使用Powershell将列插入单个目录中的多个CSV文件 - How to Insert columns into multiple CSV files in a single directory using powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM