-1

Below is my script to replace comma's in an existing CSV file. The delimiter used is | The script works perfectly fine but takes a long time to replace. Do we have a faster approach to replace all instances of comma's with nothing.

$inform = Get-Content C:\product.csv
                $inform | % { 
                $info = $_.ToString().Replace(",","")                
                $info  | Out-file C:\product_comma_replaced.csv -Append 
                }

Input CSV:

1|Test,ABC|Test,LMN

Output CSV:

1|TestABC|TestLMN
Joseph
  • 530
  • 3
  • 15
  • 37
  • So, do you want to remove commas, or do you want to [put fields with commas in double quotes](https://stackoverflow.com/q/45011963/1630171)? Which is it? – Ansgar Wiechers Jul 11 '17 at 08:16
  • put fields with commas in double quotes - This is a separate requirement, don't have the answer yet I got the answer for this post(replace comma's with nothing), it was indeed a duplicate. – Joseph Jul 11 '17 at 08:29

1 Answers1

0

Try This,

$inform = Get-Content C:\product.csv
$inform.ToString()
$y=$inform -replace ',' -replace ''
 $y  | Out-file C:\product_comma_replaced.csv -Append 
Chetan Kulkarni
  • 404
  • 4
  • 15
  • This is not recommended an approach if working with large (100s of megabytes to gigabytes or larger) files. – vonPryz Jul 11 '17 at 09:37
  • I agree Sir,But Looking at the problem defination i presumed it to be lesser than a mb/Max 10 mb and its the easiest and quickest fix i feel.Just giving out a simple fix to the simple problem. :) – Chetan Kulkarni Jul 11 '17 at 11:24
  • 1048576 rows csv translates to 600 mb,I Hope @joseph Doesnt have that huge data – Chetan Kulkarni Jul 11 '17 at 11:27