Author Posts

January 17, 2018 at 10:52 pm

I am getting a text file where I need to read the 1st ll positions in each line of the file. If a "-" is found I need to replace that with a ",".

I do not want to change any other "-" within the file only the 1st one. The "-" can float between position 5 & 9.

Is there a way to accomplish this

January 17, 2018 at 10:56 pm

One way would be to convert the string to an array of Char (characters), and the enumerate the positions you care about. Regular expressions would also be an approach you could take.

January 18, 2018 at 2:46 pm

I think I figured it out. Since I need the very first instance of the "-" to a "," and nothing else...I used this line.

$content | Foreach-object { $_ -replace '^(.*?)-', '$1,'} | Set-content $file

It appears to be working and made it very simple.