使用Powershell填充csv文件中的特定列

时间:2019-03-20 19:34:35

标签: powershell

正如标题所述,我想通过在CSV文件的底部添加一个特定的列来填充它。

假设我们有一个如下所示的CSV表(file.csv):

     A       B        C              D
1 "text"   "word"  "phrase"       "term"
2 "number" "digit" "amount"       "numeral"
3 "letter" "char"  "hieroglyphic" "symbol"

在我的PowerShell脚本中,我有:

$foo = "x"

Out-File -FilePath file.CSV -Append -InputObject $foo

结果是这样的:

     A       B        C              D
1 "text"   "word"  "phrase"       "term"
2 "number" "digit" "amount"       "numeral"
3 "letter" "char"  "hieroglyphic" "symbol"
4   "x"

如何将$foo的值放入特定列,例如C(在下面)?

     A       B        C              D
1 "text"   "word"  "phrase"       "term"
2 "number" "digit" "amount"       "numeral"
3 "letter" "char"  "hieroglyphic" "symbol"
4                    "x"

1 个答案:

答案 0 :(得分:0)

将要存储该值的列号(从索引0开始)的值放在变量column_number中

$column_number = 1

您可以按以下方式使用Add-Content命令

Add-Content .\file.CSV -Value "$(","*$column_number)$foo"

这会将存储在$foo中的值附加到指定的列号。 Here是“添加内容”命令的文档。