Good Afternoon,<\/p>\n
I’m working with an insanely huge csv file that I’m in process of trimming out the extra columns that aren’t needed via powershell. My issue is that there are multiple address cells, which can have commas in the individual cells.<\/p>\n
I’d like to have a more elegant solution than:<\/p>\n
$FileData | ForEach-Object{if($_.Address116){$_.Address116 = $_.Address116.replace(',','')}}\n$FileData | ForEach-Object{if($_.Address217){$_.Address217 = $_.Address217.replace(',','')}}\n$FileData | ForEach-Object{if($_.Address117){$_.Address117 = $_.Address117.replace(',','')}}\n$FileData | ForEach-Object{if($_.Address130){$_.Address130 = $_.Address130.replace(',','')}}\n$FileData | Foreach-object{if($_.PartnerPlanID155){$_.PartnerPlanID155 = $_.PartnerPlanID155.replace(',','')}}\n<\/code><\/pre>\nThis works, but I need to individually identify each column…<\/p>\n
I’d like something like this, but it doesn’t work:<\/p>\n
$ENHeaders = [System.Collections.ArrayList]@('Address116';'Address217';'Address117')\n\n$FileData | foreach($x -in $enheaders){ForEach-Object{$_.($x) = $_.($x) -replace \",\",\"\"}}\n<\/code><\/pre>\n$ENHeaders is a lot longer, trimmed it down for example purposes.<\/p>\n
What am I missing here? I think it’s where I’m trying to identify the $x in the foreach, and using it as the column name in the foreach-object.<\/p>","upvoteCount":1,"answerCount":2,"datePublished":"2018-01-17T19:39:17.000Z","author":{"@type":"Person","name":"ryanstanford2","url":"https://community.spiceworks.com/u/ryanstanford2"},"acceptedAnswer":{"@type":"Answer","text":"
I realized I had it out of order some… I got it to work with the code below:<\/p>\n
foreach($x in $enheaders){\n$FileData | ForEach-Object{if(($_.($x))){($_.($x)) = ($_.($x)).replace(',','')}}\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-01-17T20:28:02.000Z","url":"https://community.spiceworks.com/t/commas-in-cells-in-a-csv-remove-all/629281/2","author":{"@type":"Person","name":"ryanstanford2","url":"https://community.spiceworks.com/u/ryanstanford2"}},"suggestedAnswer":[{"@type":"Answer","text":"Good Afternoon,<\/p>\n
I’m working with an insanely huge csv file that I’m in process of trimming out the extra columns that aren’t needed via powershell. My issue is that there are multiple address cells, which can have commas in the individual cells.<\/p>\n
I’d like to have a more elegant solution than:<\/p>\n
$FileData | ForEach-Object{if($_.Address116){$_.Address116 = $_.Address116.replace(',','')}}\n$FileData | ForEach-Object{if($_.Address217){$_.Address217 = $_.Address217.replace(',','')}}\n$FileData | ForEach-Object{if($_.Address117){$_.Address117 = $_.Address117.replace(',','')}}\n$FileData | ForEach-Object{if($_.Address130){$_.Address130 = $_.Address130.replace(',','')}}\n$FileData | Foreach-object{if($_.PartnerPlanID155){$_.PartnerPlanID155 = $_.PartnerPlanID155.replace(',','')}}\n<\/code><\/pre>\nThis works, but I need to individually identify each column…<\/p>\n
I’d like something like this, but it doesn’t work:<\/p>\n
$ENHeaders = [System.Collections.ArrayList]@('Address116';'Address217';'Address117')\n\n$FileData | foreach($x -in $enheaders){ForEach-Object{$_.($x) = $_.($x) -replace \",\",\"\"}}\n<\/code><\/pre>\n$ENHeaders is a lot longer, trimmed it down for example purposes.<\/p>\n
What am I missing here? I think it’s where I’m trying to identify the $x in the foreach, and using it as the column name in the foreach-object.<\/p>","upvoteCount":1,"datePublished":"2018-01-17T19:39:17.000Z","url":"https://community.spiceworks.com/t/commas-in-cells-in-a-csv-remove-all/629281/1","author":{"@type":"Person","name":"ryanstanford2","url":"https://community.spiceworks.com/u/ryanstanford2"}}]}}