Reputation: 159
I have a text file with Name at position 0-10, phone at 11-20.
How can I replace a string at location 11 to contain all 9's?
I've been playing around with Get-Content
and -replace
to get familiar.
$path = "g:\test.txt"
(Get-Content $path) -replace "Name", "Forename" | Out-File $path
Example :
STARTDELIV|BA|BATCH PRINT |
INFORMAT01|[email protected] |
INFORMAT02|01021990|CRZWS|AA|2 |
INFORMAT03|Mr. John Doe|+00000 |
So say I would like to replace the name Mr. John Doe with X's , how would I prevent it replacing the same 10 bytes on every line
Upvotes: 0
Views: 65
Reputation: 23355
You could use the SubString
method to get the 10 characters of the string starting from position 11:
$Path = "g:\test.txt"
$String = (Get-Content $Path)
$StringToReplace = $String.Substring(11,10)
And then use -Replace
to replace that part of the string with all 9s (beware this assumes that string doesn't occur in that way anywhere else in the string):
$String -Replace $StringToReplace,('9'*$StringToReplace.Length)
Here's a shorter single line way to achieve the same result:
$String.Replace($String.Substring(11,10),'9'*10)
Upvotes: 1