使用 powershell 将新行添加到 csv 文件

发布于 2025-01-15 14:08:31 字数 1161 浏览 4 评论 0原文

我是 Powershell 新手。我有一个充满 CSV 文件的目录。我必须读取 csv 文件中的每一行并采取某些操作。如果目标列中的任何值等于“XXXXXXX”,我需要将整行复制到目标字段为“NEWVALUE”的新行。我不想附加输入文件,而是创建输入的克隆并对其进行更改。不知道如何使用 Powershell 执行此操作。

示例和示例输入&输出文件: 输入文件可以有任意行数,但只能有 9 列

标题:第 1 列,第 2 列......第 9 列

第 1 行:abc、xyz、123、、、、、999、完成

第 2 行:kkk,rww, zzz,888,111,,,,完成

第 3 行:xxx,rww, zzz,888,111,,XXXXXXX,,完成

第 4 行:yyy,rww, zzz,888,111,,,,完成

第 5 行:rrr,rww, zzz,888,111,,,,完成

输出文件应添加带有“NEWVALUE”的附加行。其余字段与复制的行相同

标题:第 1 列,第 2 列......第 9 列

第 1 行:abc、xyz、123、、、、、999、完成

第 2 行:kkk,rww, zzz,888,111,,,,完成

第 3 行:xxx,rww, zzz,888,111,,XXXXXXX,,完成

第 4 行:xxx,rww, zzz,888,111,,NEWVALUE,,完成

第 5 行:yyy,rww, zzz,888,111,,,,完成

第 6 行:rrr,rww, zzz,888,111,,,,完成

如果您需要更多详细信息,请告诉我。感谢您抽出时间。

I am new to Powershell. I have a directory full of CSV files. I have to read each row in the csv files and take certain actions. If any value in the target column is equal to "XXXXXXX", I need to copy the whole row to a new row with the target field as "NEWVALUE". I don't want to append the input file but create a clone of input and do changes in that. Not sure how to do this with Powershell.

Example and sample input & Output files:
Input file can have any number of rows but only 9 columns

Header: column1,column2.....column9

Row1: abc, xyz, 123,,,,,999,done

Row2: kkk,rww, zzz,888,111,,,,done

Row3: xxx,rww, zzz,888,111,,XXXXXXX,,done

Row4: yyy,rww, zzz,888,111,,,,done

Row5: rrr,rww, zzz,888,111,,,,done

Output file should add an additional row with "NEWVALUE". Rest of the fields are same as copied row

Header: column1,column2.....column9

Row1: abc, xyz, 123,,,,,999,done

Row2: kkk,rww, zzz,888,111,,,,done

Row3: xxx,rww, zzz,888,111,,XXXXXXX,,done

Row4: xxx,rww, zzz,888,111,,NEWVALUE,,done

Row5: yyy,rww, zzz,888,111,,,,done

Row6: rrr,rww, zzz,888,111,,,,done

Please let me know if you need more details. Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱不及久伴 2025-01-22 14:08:31

我会将 .csv 转换为 Excel 并进行条件格式或 if 函数。

如果 b1 =“xxxx”; b4=b1;blabla

然后再次重新转换为 .csv。

i would convert .csv to excel and make a conditional formatting or if function.

if b1 = "xxxx"; b4=b1;blabla

then reconvert to .csv again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文