将列添加到 CSV Windows PowerShell [英] Add Column to CSV Windows PowerShell
问题描述
我有一个相当标准的带有标题的 csv 文件,我想添加一个新列 &将所有行设置为相同的数据.
I have a fairly standard csv file with headers I want to add a new column & set all the rows to the same data.
column1, column2
1,b
2,c
3,5
之后
column1, column2, column3
1,b, setvalue
2,c, setvalue
3,5, setvalue
如果有人能指出我正确的方向,那我什么都找不到.抱歉,对 Power Shell 非常陌生.
I can't find anything on this if anybody could point me in the right direction that would be great. Sorry very new to Power Shell.
推荐答案
以下是使用计算属性的一种方法:
Here's one way to do that using Calculated Properties:
Import-Csv file.csv |
Select-Object *,@{Name='column3';Expression={'setvalue'}} |
Export-Csv file.csv -NoTypeInformation
您可以在此处找到有关计算属性的更多信息:http://technet.microsoft.com/en-us/library/ff730948.aspx.
You can find more on calculated properties here: http://technet.microsoft.com/en-us/library/ff730948.aspx.
简而言之,您导入文件,将内容通过管道传送到 Select-Object
cmdlet,选择所有现有属性(例如*"),然后添加一个新属性.
In a nutshell, you import the file, pipe the content to the Select-Object
cmdlet, select all exiting properties (e.g '*') then add a new one.
这篇关于将列添加到 CSV Windows PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!