user310291
user310291

Reputation: 38228

Trimming CSV column not working

I have a csv file

A;B
FR0001  ; TOTO
FR0002  ; TITI

I want to trim column A

$csv=Import-Csv "test_trim.csv" -delimiter ";" | %{$_.A = ($_.A).Trim()}

But $csv is empty why ?

Upvotes: 1

Views: 80

Answers (2)

Matt
Matt

Reputation: 46710

$csv will capture all of the output of your command. Of which there currently is none. If you changed the line to:

Import-Csv "test_trim.csv" -delimiter ";" | %{$_.A = ($_.A).Trim()}

You would see nothing on the console because there is no output. You need to output the updated object back to the pipe so that it will be captured by $csv

$csv = Import-Csv "test_trim.csv" -delimiter ";" | ForEach-Object{$_.A = ($_.A).Trim(); $_}

Upvotes: 4

Dave Sexton
Dave Sexton

Reputation: 11188

Try this:

$csv=Import-Csv "test_trim.csv" -delimiter ";" | %{$_.A = ($_.A).Trim(); $_}

Powershell will by default return the result of last operation, so make the last operation your object.

Upvotes: 3

Related Questions