powershell查找并替换每个文件中具有特定匹配项和扩展名的文本
我试着做两件事。首先我要删除匹配后的所有文本,然后用新文本替换匹配行 在下面的示例中,我希望找到动物列表的行,并将所有文档*.txt中的所有行替换为替换为this.txt中的文本 下面我的第一个foreach将删除动物列表之后的所有内容,第二个foreach将替换动物列表,从而也将在该行后面添加新内容,内容来自replaceWithThis.txt replaceWithThis.txt包含:powershell查找并替换每个文件中具有特定匹配项和扩展名的文本,powershell,replace,foreach,Powershell,Replace,Foreach,我试着做两件事。首先我要删除匹配后的所有文本,然后用新文本替换匹配行 在下面的示例中,我希望找到动物列表的行,并将所有文档*.txt中的所有行替换为替换为this.txt中的文本 下面我的第一个foreach将删除动物列表之后的所有内容,第二个foreach将替换动物列表,从而也将在该行后面添加新内容,内容来自replaceWithThis.txt replaceWithThis.txt包含: List of animals Cat 5 Lion 3 Bird 2 List of cities
List of animals
Cat 5
Lion 3
Bird 2
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
$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
}
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
*.txt包含:
List of animals
Cat 5
Lion 3
Bird 2
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
$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
}
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
代码:
List of animals
Cat 5
Lion 3
Bird 2
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
$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
}
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
所有(*.txt)的最终结果应为:
List of animals
Cat 5
Lion 3
Bird 2
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
$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
}
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
使用正则表达式,以下代码应该可以工作:
$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
}
}
正则表达式详细信息
( 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)
)
使用正则表达式,以下代码应该可以工作:
$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
}
}
正则表达式详细信息
( 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)
)
非常感谢西奥,它工作得很好!:)谢谢你对Regex的解释,它总是让我头疼,所以我通常会尽量避免它。非常感谢Theo,它工作得非常好!)谢谢你对正则表达式的解释,它总是让我头疼,所以我通常会尽量避免它。