-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
2
  • So, do you want to remove commas, or do you want to put fields with commas in double quotes? Which is it? Commented Jul 11, 2017 at 8: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. Commented Jul 11, 2017 at 8:29

1 Answer 1

0

Try This,

$inform = Get-Content C:\product.csv
$inform.ToString()
$y=$inform -replace ',' -replace ''
 $y  | Out-file C:\product_comma_replaced.csv -Append 
Sign up to request clarification or add additional context in comments.

3 Comments

This is not recommended an approach if working with large (100s of megabytes to gigabytes or larger) files.
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. :)
1048576 rows csv translates to 600 mb,I Hope @joseph Doesnt have that huge data

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.