-3

I would to remove only the fifth comma in a CSV file and remove all additional space.

i.e. Original file A,B100537 ,8438,TRUST ,VECCO 4 PORT USB 2.0 MINI HUB ,BLACK ,0000000000000009.01,14591

result A,B100537 ,8438,TRUST COMPUTER ,VECCO 4 PORT USB 2.0 MINI HUB BLACK ,0000000000000009.01,14591

Code:

$x = Get-Content file.txt 

$x | Foreach-Object {$_.Trim() -replace '^([^,]*,[^,]*,[^,]*,[^,]*,[^,]*),(.*)$', '$1 $2' -replace '\s+', ' ' }  |  Set-Content output.txt
Ngc7771
  • 7
  • 3

1 Answers1

1

I would just use a regular expression.

Get-Content Filename.csv | 
  Foreach-Object {
    $_ -replace '^([^,]*,[^,]*,[^,]*),(.*)$', '$1 $2' 
  } | Set-Content Output.csv
Bacon Bits
  • 30,782
  • 5
  • 59
  • 66