简体   繁体   English

Powershell在每个文件中查找并替换具有特定匹配和扩展名的文本

[英]powershell find and replace text in each file with specific match and extension

I am trying to do two things. 我正在尝试做两件事。 Firstly I am to remove all text after match, and secondly replace match line with new text. 首先,我要删除匹配后的所有文本,然后将匹配行替换为新文本。

In my example below I want to find the line List of animals and replace all lines following it in all documents *.txt with the text found in replaceWithThis.txt . 在下面的示例中,我想找到动物列表行,并将所有文档* .txt紧随其后的所有行替换为replaceWithThis.txt中的文本。

My first foreach below will remove everything that follows List of animals and my second foreach will replace List of animals and thus also adding new content following that line by content from replaceWithThis.txt . 我在下面的第一个foreach将会删除动物列表之后的所有内容,而我的第二个foreach将替换动物列表之后的内容,因此还将通过replaceWithThis.txt中的内容添加新内容。

replaceWithThis.txt contains: replaceWithThis.txt包含:

List of animals
Cat 5
Lion 3
Bird 2

*.txt contains: * .txt包含:

List of cities
London 2
New York 3
Beijing 6

List of car brands
Volvo 2
BMW 3
Audi 5

List of animals
Cat 1
Dog 3
Bird 7

Code: 码:

$replaceWithThis = Get-Content c:\temp\replaceWithThis.txt -Raw

$allFiles = Get-ChildItem "c:\temp" -recurse | where {$_.extension -eq ".txt"}
$line = Get-Content c:\temp\*.txt | Select-String cLuxPlayer_SaveData | Select-Object -ExpandProperty Line
foreach ($file in $allFiles)
{
    (Get-Content $file.PSPath) |
    ForEach-object { $_.Substring(15,  $_.lastIndexOf('List of animals')) } |
    Set-Content $file.PSPath
}

foreach ($file in $allFiles)
{
    (Get-Content $file.PSPath) |
    Foreach-Object { $_ -replace $line,$replaceWithThis } |
    Set-Content $file.PSPath
}

Final result in all (*.txt) should be: 所有(* .txt)的最终结果应为:

List of cities
London 2
New York 3
Beijing 6

List of car brands
Volvo 2
BMW 3
Audi 5

List of animals
Cat 5
Lion 3
Bird 2

Using Regular Expression, the below code should work: 使用正则表达式,下面的代码应该可以工作:

$filesPath       = 'c:\temp'
$replaceFile     = 'c:\temp\replaceWithThis.txt'
$regexToFind     = '(?sm)(List of animals(?:(?!List).)*)'
$replaceWithThis = (Get-Content -Path $replaceFile -Raw).Trim()

Get-ChildItem -Path $filesPath -Filter *.txt | ForEach-Object {
    $content = $_ | Get-Content -Raw
    if ($content -match $regexToFind) {
        Write-Host "Replacing text in file '$($_.FullName)'"
        $_ | Set-Content -Value ($content -replace $matches[1].Trim(), $replaceWithThis) -Force
    }
}

Regex Details 正则表达式详细信息

(                      Match the regular expression below and capture its match into backreference number 1
   List of animals     Match the characters “List of animals” literally
   (?:                 Match the regular expression below
      (?!              Assert that it is impossible to match the regex below starting at this position (negative lookahead)
         List          Match the characters “List” literally
      )
      .                Match any single character
   )*                  Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM