Replace the first occurence of a text in a given input using Powershell script -
the following code works, replaces occurrences of text finds. there way replace first occurrence. have tried 4 days now.
please help.
cls $filename = "c:\test\bmsbackuplog.htm" $findstring=select-string $filename -pattern "the backup of volume system reserved" -list $replacement = "<tr><td>change using line</td></tr>" $firstdata=$findstring[0].line $both="$firstdata$replacement" $match ="the backup of volume system reserved*.*" (get-content ($filename)) -replace $match,$both | set-content "c:\test\bmsbackuplog.htm"
i use zero-width negative lookbehind assertion that:
$filename = "c:\test\bmsbackuplog.htm" $pattern = 'the backup of volume system reserved' $replacement = '$1<tr><td>change using line</td></tr>' [io.file]::readalltext($filename) -replace "(?<!$pattern[\s\s]*)($pattern)", $replacement
Comments
Post a Comment