[英]Add a new row to to csv file using powershell
I am new to Powershell. I have a directory full of CSV files.我是 Powershell 的新手。我有一个包含 CSV 个文件的目录。 I have to read each row in the csv files and take certain actions.我必须阅读 csv 文件中的每一行并采取某些措施。 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".如果目标列中的任何值等于“XXXXXXX”,我需要将整行复制到目标字段为“NEWVALUE”的新行。 I don't want to append the input file but create a clone of input and do changes in that.我不想 append 输入文件,而是创建输入的克隆并在其中进行更改。 Not sure how to do this with Powershell.不确定如何使用 Powershell 执行此操作。
Example and sample input & Output files: Input file can have any number of rows but only 9 columns示例和示例输入 & Output 文件:输入文件可以有任意数量的行,但只有 9 列
Header: column1,column2.....column9 Header: column1,column2.....column9
Row1: abc, xyz, 123,,,,,999,done Row1: abc, xyz, 123,,,,,999,完成
Row2: kkk,rww, zzz,888,111,,,,done Row2: kkk,rww, zzz,888,111,,,,done
Row3: xxx,rww, zzz,888,111,,XXXXXXX,,done Row3: xxx,rww, zzz,888,111,,XXXXXXX,,完成
Row4: yyy,rww, zzz,888,111,,,,done Row4: yyy,rww, zzz,888,111,,,,完成
Row5: rrr,rww, zzz,888,111,,,,done Row5: rrr,rww, zzz,888,111,,,,done
Output file should add an additional row with "NEWVALUE". Output 文件应添加带有“NEWVALUE”的附加行。 Rest of the fields are same as copied row Rest 的字段与复制的行相同
Header: column1,column2.....column9 Header: column1,column2.....column9
Row1: abc, xyz, 123,,,,,999,done Row1: abc, xyz, 123,,,,,999,完成
Row2: kkk,rww, zzz,888,111,,,,done Row2: kkk,rww, zzz,888,111,,,,done
Row3: xxx,rww, zzz,888,111,,XXXXXXX,,done Row3: xxx,rww, zzz,888,111,,XXXXXXX,,完成
Row4: xxx,rww, zzz,888,111,,NEWVALUE,,done Row4: xxx,rww, zzz,888,111,,NEWVALUE,,done
Row5: yyy,rww, zzz,888,111,,,,done Row5: yyy,rww, zzz,888,111,,,,完成
Row6: rrr,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.谢谢你的时间。
i would convert.csv to excel and make a conditional formatting or if function.我会将 csv 转换为 excel 并进行条件格式化,或者如果是 function。
if b1 = "xxxx";如果 b1 = "xxxx"; b4=b1;blabla b4=b1;blabla
then reconvert to.csv again.然后再次转换为.csv。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.