I have text1.txt file with following content:
sometext_1
--start
sometext_2.1
sometext_2.2
...
sometext_2.n
--end
sometext_3
and text2.txt file with following content:
--start
sometext_2_new1
sometext_2_new2
...
sometext_2_newn
--end
Could someone help me to write Powershell code to update content of text1.txt file between keywords --start and --end with all content of file text2.txt? So the new text1.txt will look like below:
sometext_1
--start
sometext_2_new1
sometext_2_new2
...
sometext_2_newn
--end
sometext_3
UPDATE: I'm really new in Powershell. After some research I was able to came up with following which is still not giving me what I need:
param(
[string]$location
)
$contentStart = Get-Content "c:\text2.txt"
$configFiles = Get-ChildItem $location\*.txt -rec
foreach ($file in $configFiles)
{
(Get-Content $file.PSPath) |
Foreach-Object { $_ -replace "--start.*?(--end)", $contentStart } |
Out-File $file.PSPath
}
But it works only if text1.txt has keywords --start and --end in one line and relult is following (inserted content do not have endline symbols):
sometext_1
--start sometext_2_new1 sometext_2_new2 ... sometext_2_newn --end
sometext_3