PowerTip: Using PowerShell to Convert raw Text to a Comma Separated columm

Doctor Scripto

Summary: Using PowerShell and a very simple Regular Expression to convert Random counts of spaces to a Comma

A picture containing scissors Description automatically generated

Hey Doctor Scripto! I have some string based output from a non PowerShell application. The output is good but I need to easily parse it. Is there a way to convert that data to something consistent like “Comma Separated?”

Why yes you can! This is something that is easily done with a regular expression. Using a simple example like this will identify all content with more than one blank space separating it, and replace it with a comma!

$SampleText=’Hello             This                      is   your      Good    Friend     Doctor                     Scripto’ $SampleText -replace ‘\s{2,}’ , ‘,’

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Forum. See you tomorrow. Until then, peace.

Your good friend, Doctor Scripto

PowerShell, Doctor Scripto, PowerTip, Dan Reist, Regular Expressions



Discussion is closed.

Feedback usabilla icon