Powershell search for every instance of string and then replace entire line

StackOverflow https://stackoverflow.com/questions/22966027

  •  30-06-2023
  •  | 
  •  

Question

my first post so hope this goes well! :)

I'm trying to use powershell to search through a file for a pattern and replace the entire line if it matches that pattern.

In the target file there are 2 instances of the pattern. The code below only works if there is one instance of the pattern 'jmsPassword':

$file='C:\Temp\ClientConduit.xml'

$line=Select-String $file -pattern "jmsPassword"
$line=$line.linenumber

$content = Get-Content $file
$content|
ForEach-Object {
    if ($_.ReadCount -eq $line) {
        $_ -replace '^.+','           <PROPERTY propname="jmsPassword" proptype="STRING" propvalue="!abcd1234"/>'
   } else {
    $_
   }
  } | Set-Content $file

Any ideas how I can get all instances replaced?

A sample section of text that needs editing looks as follows:

         <TECHNICIAN class="ParameterTechnician" compressvalets="N" encryptvalets="N" export="Y" name="ParameterTechnician" package="com.extendyourstore.foundation.manager.parameter" singleton="N">
       <PROPERTY propname="configScript" proptype="STRING" propvalue="classpath://config/manager/PosParameterTechnician.xml"/>
       <PROPERTY propname="topicName" proptype="STRING" propvalue="jms/parameters"/>
       <PROPERTY propname="listenForUpdates" proptype="STRING" propvalue="Y"/>

      <!-- comment out following variables if using JBoss -->
       <PROPERTY propname="clientID" proptype="STRING" propvalue="REG103"/>
       <PROPERTY propname="jmsPassword" proptype="STRING" propvalue="MDAxMaiyktgunPc0NKx7QAe3g"/>
       <PROPERTY propname="topicName" proptype="STRING" propvalue="jms/files"/>
       <PROPERTY propname="listenForUpdates" proptype="STRING" propvalue="Y"/>
       <PROPERTY propname="jmsPassword" proptype="STRING" propvalue="MDAxMaityhdxdfc0NKx7QAe3g"/>
       <PROPERTY propname="durableSubscriber" proptype="STRING" propvalue="Y"/>

The resulting affected lines should end up like this:

         <TECHNICIAN class="ParameterTechnician" compressvalets="N" encryptvalets="N" export="Y" name="ParameterTechnician" package="com.extendyourstore.foundation.manager.parameter" singleton="N">
       <PROPERTY propname="configScript" proptype="STRING" propvalue="classpath://config/manager/PosParameterTechnician.xml"/>
       <PROPERTY propname="topicName" proptype="STRING" propvalue="jms/parameters"/>
       <PROPERTY propname="listenForUpdates" proptype="STRING" propvalue="Y"/>

      <!-- comment out following variables if using JBoss -->
       <PROPERTY propname="clientID" proptype="STRING" propvalue="REG103"/>
       <PROPERTY propname="jmsPassword" proptype="STRING" propvalue="!abcd1234"/>
       <PROPERTY propname="topicName" proptype="STRING" propvalue="jms/files"/>
       <PROPERTY propname="listenForUpdates" proptype="STRING" propvalue="Y"/>
       <PROPERTY propname="jmsPassword" proptype="STRING" propvalue="!abcd1234"/>
       <PROPERTY propname="durableSubscriber" proptype="STRING" propvalue="Y"/>

Thanks!

Était-ce utile?

La solution

$file='C:\Temp\ClientConduit.xml'
$replace='           <PROPERTY propname="jmsPassword" proptype="STRING" propvalue="!abcd1234"/>'
(Get-Content $file) | % {
    if($_ -like "*jmsPassword*") {
        $_ -replace '^.+',$replace
    }
    else {
        $_
    }
} | set-content $file
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top