如何使用PowerShell将数据添加到CSV的最后一列? [英] How to add data to last column of CSV using PowerShell?

查看:360
本文介绍了如何使用PowerShell将数据添加到CSV的最后一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将数据添加到CSV文件的最后一列。我拥有的测试文件如下:

I have a requirement to add data to the last column of a CSV file. The test file I have looks like:


NAME,AGE,OFFICE,DEPT,SKILL
jack,24,IBM,Retail
tom,32,MS,BFSI
SAM,44,MGR,Test

我设法解析了CSV,但是很难将数据添加到最后一列 SKILL。要求是将单词 Java添加到每一行的最后一列

I have managed to parse the CSV but then adding data to the last column "SKILL" is difficult. The requirement is to add the word 'Java' to the last column on each row


NAME,AGE,OFFICE,DEPT,SKILL
jack,24,IBM,Retail,Java
tom,32,MS,BFSI,Java
SAM,44,MGR,Test,Java

请注意,添加到最后一列的值在行中保持不变。

Please note that the value added to the last column remains the same across rows.

推荐答案

您可以更改导入对象的 SKILL 属性的值,并通过以下方法导出到CSV文件:

You can change the value of SKILL property of the imported objects, and export to CSV file by this:

Import-Csv test.txt |
    ForEach-Object {$_.SKILL = "Java"; $_} |
    Export-Csv test_out.txt -NoTypeInformation

但是导出- Csv 在值周围加上引号,因此 test_out.txt 看起来像这样:

However Export-Csv adds quotation marks around the values, so test_out.txt would look like this:

"NAME","AGE","OFFICE","DEPT","SKILL"
"jack","24","IBM","Retail","Java"
"tom","32","MS","BFSI","Java"
"SAM","44","MGR","Test","Java"

也许您应该只添加,Java 到每行第二行的结尾:

Maybe you should simply add ",Java" to the end of each line second line onwards:

Get-Content test.txt |
    ForEach-Object { if($_.ReadCount -gt 1) { "$($_),Java" } else { $_ } } |
    Out-File test_out.txt

这篇关于如何使用PowerShell将数据添加到CSV的最后一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆