Replace a text part in textfile: Unterschied zwischen den Versionen

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „== Goal == Replace the text part between ''### Test:'' and ''###''. == Regex == (?<=<YourStartString>)(.|\n)*(?=<YourEndString>) Example for a text part betw…“)
 
 
(5 dazwischenliegende Versionen des gleichen Benutzers werden nicht angezeigt)
Zeile 4: Zeile 4:
 
== Regex ==
 
== Regex ==
 
  (?<=<YourStartString>)(.|\n)*(?=<YourEndString>)
 
  (?<=<YourStartString>)(.|\n)*(?=<YourEndString>)
Example for a text part between:
+
Example for a text part:
 +
### Test
 +
This Text will
 +
changed with the following
 +
PowerShell Script
 +
###
 +
Regex for the example text part below:
 
  (?<=\#\#\#\.Test)(.|\n)*(?=\#\#\#)
 
  (?<=\#\#\#\.Test)(.|\n)*(?=\#\#\#)
  
### Test
 
This Text will
 
changed with the following
 
PowerShell Skript
 
###
 
  
 +
== PowerShell Script ==
 +
=== File to change - Before the PowerShell Script===
 +
Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
 +
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
 +
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
 +
no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
 +
consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
 +
sed diam voluptua.
 +
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
 +
no sea takimata sanctus est Lorem ipsum dolor sit amet.
 +
 +
### Test
 +
This Text will
 +
changed with the following
 +
PowerShell Script
 +
###
  
  
== PowerShell Skript ===
+
=== PowerShell Script to change text part in a file ===
 
<source lang="powershell">
 
<source lang="powershell">
 
$FileToChangePath = "C:\Temp\FileToChange.txt"
 
$FileToChangePath = "C:\Temp\FileToChange.txt"
Zeile 25: Zeile 42:
 
$RegexReplace = "(?<=\#\#\#\.Test)(.|\n)*(?=\#\#\#)"
 
$RegexReplace = "(?<=\#\#\#\.Test)(.|\n)*(?=\#\#\#)"
  
If ($ZabbixConf -match $RegexSearch) {
+
If ($FileToChange -match $RegexSearch) {
 
     Write-Host "The text part in the file has been changed." -ForegroundColor Green
 
     Write-Host "The text part in the file has been changed." -ForegroundColor Green
 
     $ChangeTextPart = $FileToChange -replace $RegexReplace, ("`r`n" + $TemplateTextPartPath + "`r`n")
 
     $ChangeTextPart = $FileToChange -replace $RegexReplace, ("`r`n" + $TemplateTextPartPath + "`r`n")
Zeile 34: Zeile 51:
 
}
 
}
 
</source>
 
</source>
 +
 +
 +
=== Text part in the file has been changed - After the PowerShell Script===
 +
Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
 +
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
 +
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
 +
no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
 +
consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
 +
sed diam voluptua.
 +
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
 +
no sea takimata sanctus est Lorem ipsum dolor sit amet.
 +
 +
### Test
 +
This Text is
 +
the changed text
 +
with the PowerShell Script
 +
###''
 +
 +
 +
[[Datei:Regex-text-part.png]]
  
  
Zeile 42: Zeile 79:
  
  
[[http://wiki.webperfect.ch/index.php?title=Kategorie:PowerShell]]
+
[[Kategorie:PowerShell]]

Aktuelle Version vom 27. Juni 2018, 15:42 Uhr

Goal

Replace the text part between ### Test: and ###.

Regex

(?<=<YourStartString>)(.|\n)*(?=<YourEndString>)

Example for a text part:

### Test
This Text will
changed with the following
PowerShell Script
###

Regex for the example text part below:

(?<=\#\#\#\.Test)(.|\n)*(?=\#\#\#)


PowerShell Script

File to change - Before the PowerShell Script

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, 
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, 
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, 
no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, 
consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, 
sed diam voluptua. 
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, 
no sea takimata sanctus est Lorem ipsum dolor sit amet.

### Test
This Text will
changed with the following
PowerShell Script
###


PowerShell Script to change text part in a file

$FileToChangePath = "C:\Temp\FileToChange.txt"
$FileToChange = Get-Content $FileToChangePath -Raw
 
$TemplateTextPartPath = Get-Content "C:\Temp\TemplateTextPart.txt" -Raw
 
$RegexSearch = "### Test" #Search the head of text part
$RegexReplace = "(?<=\#\#\#\.Test)(.|\n)*(?=\#\#\#)"
 
If ($FileToChange -match $RegexSearch) {
    Write-Host "The text part in the file has been changed." -ForegroundColor Green
    $ChangeTextPart = $FileToChange -replace $RegexReplace, ("`r`n" + $TemplateTextPartPath + "`r`n")
    Set-Content -Path $FileToChangePath -Value $ChangeTextPart
} Else {
    Write-Host "The text part in the file has been added." -ForegroundColor Yellow
    Add-Content -Path $FileToChangePath -Value ("`r`n" + "`r`n" + "`r`n" + "`r`n" + "### Test" + "`r`n" + $TemplateTextPartPath + "`r`n" + "###" ) -PassThru
}


Text part in the file has been changed - After the PowerShell Script

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, 
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, 
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, 
no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, 
consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, 
sed diam voluptua. 
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, 
no sea takimata sanctus est Lorem ipsum dolor sit amet.

### Test
This Text is
the changed text
with the PowerShell Script
###


Regex-text-part.png


Regex101: https://regex101.com/