JanS
JanS

Reputation: 21

Powershell how to add data in the csv file to one column

I have a csv file with a few columns. For example

col1;col2;col3;col4  
text1;text2;text3;text4  
text5;text6;text7;text8  
text9;text10;text11;text12

I want to add text to column col3, like this:

col1;col2;col3;col4  
text1;text2;append\text3;text4  
text5;text6;append\text7;text8  
text9;text10;append\text11;text12

So question is:
How to do this?

(I'm stuck whit that how I add data to each column in col3.)

Upvotes: 2

Views: 19342

Answers (2)

stej
stej

Reputation: 29449

You probably know how to read/write data. To change the data, send them to Foreach-Object, alter the data and pass the object further to Export-Csv.

Import-Csv d:\temp\so\csv1.txt  -Delimiter ';' | 
   ForEach-Object { $_.col3 = 'append\' +$_.col3; $_ } | 
   Export-Csv d:\temp\so\csv2.txt -Delimiter ';'

Upvotes: 3

Keith Hill
Keith Hill

Reputation: 201622

Give this a whirl:

Import-Csv -Delim ';' cols.csv | 
    ForEach-Object {$_.col3 = "prepend\$($_.col3)";$_} |
    Export-Csv cols2.csv -Delim ';' -NoTypeInformation

Use the -NoTypeInformation parameter to avoid this comment getting put at the top of your CSV:

#TYPE System.Management.Automation.PSCustomObject

However, if you don't mind the comment then you can leave off the -NoTypeInformation parameter.

Upvotes: 3

Related Questions