0

I'm trying to parse some very badly delimited files and ultimately change them to a CSV. I'm looking to match on number, whitespace, and then letter, and replace the whitespace with a comma.

For example If I had the line '08:34:45 home' I'd like it to recognize the '5 h' and make it '08:34:45,home'. I understand why what I have below isn't working correctly, but can someone explain how to tell Powershell that I want to keep the \d and the \D?

Get-Content -path C:\file.txt |

ForEach-Object {$_ -replace "(\d\s\D)",','}
1
  • Figured it out: ForEach-Object {$_ -replace "(\d)\s(\D)",'$1,$2'} Commented Oct 22, 2014 at 20:33

1 Answer 1

2

What you put in parentheses ( ) is captured by the regular expression, and you can access it with $1 for the first set, $2 for the second set, etc.

so you could try two capture groups:

$_ -replace '(\d)\s(\D)', '$1,$2'

See also: http://www.powershelladmin.com/wiki/Powershell_regular_expressions#Example_-_Replace_With_Captures for more details.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.