Powershell将文本添加到cvs文件
我有一个cvs文件,标题/列Powershell将文本添加到cvs文件,powershell,Powershell,我有一个cvs文件,标题/列channelrows和文本/值play和work。我必须添加硬文件。 这样它就变成了努力工作和努力玩耍。 我该怎么做 我的代码 $channelrows = Import-Csv "D:\Powershell_Project_2\newchannels.csv" $channelrows | ForEach-Object { $TheOldInput = $_.channelrows ; [PSCustomObject]@{channelrows
channelrows
和文本/值play
和work
。我必须添加硬文件
。
这样它就变成了努力工作
和努力玩耍
。
我该怎么做
我的代码
$channelrows = Import-Csv "D:\Powershell_Project_2\newchannels.csv"
$channelrows | ForEach-Object { $TheOldInput = $_.channelrows ; [PSCustomObject]@{channelrows = "$TheOldInput Hard"}}
输出
channelrows
-----------
Hard
Hard
我需要的是这个
channelrows
-----------
Work Hard
PLay Hard
如果CSV文件中的唯一列是“channelrows”,则这将非常简单:
$channelrows = Import-Csv -Path "D:\Powershell_Project_2\newchannels.csv" |
Select-Object @{Name = 'channelrows'; Expression = {'{0} hard' -f $_.channelrows}}
# output on screen
$channelrows
# output to new CSV file
$channelrows | Export-Csv -Path "D:\Powershell_Project_2\updatedchannels.csv"
如果文件中有更多列,则可以使用以下选项:
$channelrows = Import-Csv -Path "D:\Powershell_Project_2\newchannels.csv" |
Select-Object *, @{Name = 'channelrows'; Expression = {'{0} hard' -f $_.channelrows}} -ExcludeProperty channelrows
# output on screen
$channelrows
# output to new CSV file
$channelrows | Export-Csv -Path "D:\Powershell_Project_2\updatedchannels.csv"
如果您的输入csv文件看起来像 channelrows work play
列
channelrows
是csv中唯一的列吗?如果不是这样的话,请给我们看前3或4行。channelrows
是唯一的一行。@KingVince I本来想说“不客气”,但你现在没有接受答案。有什么东西对你不起作用吗?我得到的是“channelrows-----------hard-hard”@KingVince看我的编辑。对我来说,它就像你想要的一样。。请检查输入文件的内容D:\Powershell\u Project\u 2\newchannels.csv
。我想这个文件已经被你以前的尝试覆盖了。当我执行$channelrows时,我只得到硬的
,就像我在代码中的输出一样(它是一样的)@KingVince不,你的输入文件肯定已经被覆盖了,并且不是你想象的那样。新建它,然后运行代码。
channelrows
-----------
work hard
play hard