Author Posts

March 21, 2018 at 8:39 pm

I need to replace the dollar amount in the last line of a file with $a (c:\myFile.txt)
$a = 1000 replace substring(3,4) replace 0500 with 1000

file looks like:

gadfg
agdaga
0000500

March 21, 2018 at 9:59 pm

Please share your code, and the issue/question you have about it.

March 22, 2018 at 12:08 pm

I think you will need to read the file, make the replacement, then write the file out. Look for a few threads earlier this month on this forum for code examples/solutions.

March 22, 2018 at 12:50 pm

if 0500 only occurs once in the text file you could try

(get-content c:\test\replace.txt) | foreach-object {$_ -replace "0500", "1000"} | set-content c:\test\replace.txt

March 22, 2018 at 1:05 pm

maybe something like this:


$file = @"
gadfg
agdaga
0000500
"@
$file | out-file c:\temp\tempfile.txt

$content = get-content c:\temp\tempfile.txt 

$newcontent = $content | select -SkipLast 1
$newcontent += ($content | select -Last 1) -replace ("0500","1000")