I have a Config file and I want to replace all the lines between two lines using powershell Here is my code:
$HttpPath = "C:\Oracle\Middleware\Oracle_Home\user_projects\domains\my_domain\config\fmwconfig\components\OHS\ohs1\httpd.conf"
$NewLine = 'Options -Indexes'
$Pattern = '(?<=<Directory />).*?(?=</Directory>)'
(Get-Content -Path $HttpPath -Raw) | ForEach-Object {
$_ -replace $Pattern,$NewLine
} | Set-Content -Path $HttpPath
Here is text of config the file:
<Directory />
AllowOverride none
Require all denied
</Directory>
I want to replace two lines of "Hi" and "Hello" with one line "Options -Indexes". Result should look like:
<Directory />
Options -Indexes
</Directory>
This script works if the file content be like:
<Directory /> AllowOverride none Require all denied </Directory>
Then Output be like:
<Directory />Options -Indexes</Directory>
But as the content of file splits in separate lines, this does not works.
<Directory />is a self-closed tag and</Directory>is a close tag... Anyways, you might try to useGet-Content -Rawto get the file as a single text[String]rather than an array of lines ([String[]]).