2

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.)

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The -NoTypeInformation is now default behavior.
3

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 ';'

2 Comments

Thank you very much! I have even tried this code before but there seems to be that I have few code format errors...Need to study more. Problem solved!
No problem :) I see that we both with Keith added the same answer. Funny ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.