0

I have a number of files, each with a number of lines of plain text in them and I'd like to insert a space between certain "words".

I have no problem looping the files, or replacing some text with different text, but not sure how to keep the existing text when I do so!

$ln = "20142301 Starting_LOC1SVR14"
$newln = $ln -replace "_", " "
$newln = $newln -replace "LOC[0-9]","????"

Given this sample, I want to insert the space between LOC1 and SVR14 to give LOC1 SVR14

Note that the LOC goes up to 16, but I can write the regex for 1 or more numerals, it's keeping that LOC1 part thats giving me the headache!

1
  • Sorry, gave me 20142301 Starting_\0 SVR14? Commented Jan 23, 2014 at 6:44

1 Answer 1

2

Look up regexp capture groups. They are used to save the matching string into a variable that can be used later like so,

$newln -replace "LOC[0-9]+","$0 " # $0 is the match, so replace the match with "match "
20142301 Starting LOC1 SVR14
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.