String 在Windows中替换多个文件中的多个模式的选项
Win2003服务器中的源文件夹包含150000个文件,大小约为4GB。需要递归地替换XML文件中的一些模式String 在Windows中替换多个文件中的多个模式的选项,string,perl,powershell,replace,String,Perl,Powershell,Replace,Win2003服务器中的源文件夹包含150000个文件,大小约为4GB。需要递归地替换XML文件中的一些模式 $files = Get-ChildItem "source_folder" -Filter *.xml -Recurse Write-Host $files.count "files present in source" foreach ($file in $files) { (Get-Content $file.PSPath) | Foreach-
$files = Get-ChildItem "source_folder" -Filter *.xml -Recurse
Write-Host $files.count "files present in source"
foreach ($file in $files) {
(Get-Content $file.PSPath) |
Foreach-Object { $_ -replace "string1", "replacement1" } | Foreach-Object { $_ -replace "string2", "replacement2" } | Set-Content $file.PSPath
Write-Host $file.PSPath " modified"
}
此代码需要一个多小时才能完成。实现这一目标的最佳方式是什么?减少所需时间的选项有哪些?使用PERL做这件事更好吗?建议会有很大帮助 您可以使用perl一行程序—类似于:
perl -p -i -e 's/oldstring/newstring/g' `grep -ril --include *.xml oldstring *`
如果要保留原始文件的备份,请执行以下操作:
perl -p -i'.bak' -e 's/oldstring/newstring/g' `grep -ril --include *.xml oldstring *`
您可以使用perl单行程序—类似于:
perl -p -i -e 's/oldstring/newstring/g' `grep -ril --include *.xml oldstring *`
如果要保留原始文件的备份,请执行以下操作:
perl -p -i'.bak' -e 's/oldstring/newstring/g' `grep -ril --include *.xml oldstring *`
虽然我没有相同的资源来测试这一点,但我怀疑如果您避免使用这么多管道,它会运行得更快,比如:
$files = Get-ChildItem "source_folder" -Filter *.xml -Recurse
Write-Host $files.count "files present in source"
foreach ($file in $files) {
$s = Get-Content $file.PSPath
$s = $s -replace "string1", "replacement1") -replace "string2", "replacement2"
Set-Content $file.PSPath -Value $s
Write-Host $file.PSPath " modified"
}
foreach ($file in $files) {
$content = [System.IO.File]::ReadAllText($file).Replace("val1","val2")
[System.IO.File]::WriteAllText($file, $content)
Write-Host $file.PSPath " modified"
}
$files = Get-ChildItem "C:\temp" -Filter *.xml -Recurse -File
foreach ($file in $files)
{
(Get-Content $file.FullName) | Foreach-Object {
$_ -replace 'something1', 'something1aa' `
-replace 'something2', 'something2bb' `
-replace 'something3', 'something3cc' `
-replace 'something4', 'something4dd' `
-replace 'something5', 'something5dsf' `
-replace 'something6', 'something6dfsfds'
} | Set-Content $file.FullName
Write-Host $file.FullName " analysed"
}
管道机制有一些开销,这应该避免。我很想听听这对你的案子有多大影响
还有一点,最好在Set Content命令中添加一个
-Encoding
值,以控制输出文件的编码方式 虽然我没有相同的资源来测试这一点,但我怀疑如果你避开这么多管道,它会运行得更快,比如:
$files = Get-ChildItem "source_folder" -Filter *.xml -Recurse
Write-Host $files.count "files present in source"
foreach ($file in $files) {
$s = Get-Content $file.PSPath
$s = $s -replace "string1", "replacement1") -replace "string2", "replacement2"
Set-Content $file.PSPath -Value $s
Write-Host $file.PSPath " modified"
}
foreach ($file in $files) {
$content = [System.IO.File]::ReadAllText($file).Replace("val1","val2")
[System.IO.File]::WriteAllText($file, $content)
Write-Host $file.PSPath " modified"
}
$files = Get-ChildItem "C:\temp" -Filter *.xml -Recurse -File
foreach ($file in $files)
{
(Get-Content $file.FullName) | Foreach-Object {
$_ -replace 'something1', 'something1aa' `
-replace 'something2', 'something2bb' `
-replace 'something3', 'something3cc' `
-replace 'something4', 'something4dd' `
-replace 'something5', 'something5dsf' `
-replace 'something6', 'something6dfsfds'
} | Set-Content $file.FullName
Write-Host $file.FullName " analysed"
}
管道机制有一些开销,这应该避免。我很想听听这对你的案子有多大影响
还有一点,最好在Set Content命令中添加一个
-Encoding
值,以控制输出文件的编码方式 首先,您应该为此使用.NET类。
单凭这一点就可以节省你很多时间。另外,您确实应该使用v3版本替换(即.replace方法)。它更快
所以看起来是这样的:
$files = Get-ChildItem "source_folder" -Filter *.xml -Recurse
Write-Host $files.count "files present in source"
foreach ($file in $files) {
$s = Get-Content $file.PSPath
$s = $s -replace "string1", "replacement1") -replace "string2", "replacement2"
Set-Content $file.PSPath -Value $s
Write-Host $file.PSPath " modified"
}
foreach ($file in $files) {
$content = [System.IO.File]::ReadAllText($file).Replace("val1","val2")
[System.IO.File]::WriteAllText($file, $content)
Write-Host $file.PSPath " modified"
}
$files = Get-ChildItem "C:\temp" -Filter *.xml -Recurse -File
foreach ($file in $files)
{
(Get-Content $file.FullName) | Foreach-Object {
$_ -replace 'something1', 'something1aa' `
-replace 'something2', 'something2bb' `
-replace 'something3', 'something3cc' `
-replace 'something4', 'something4dd' `
-replace 'something5', 'something5dsf' `
-replace 'something6', 'something6dfsfds'
} | Set-Content $file.FullName
Write-Host $file.FullName " analysed"
}
首先,您应该为此使用.NET类。 单凭这一点就可以节省你很多时间。另外,您确实应该使用v3版本替换(即.replace方法)。它更快 所以看起来是这样的:
$files = Get-ChildItem "source_folder" -Filter *.xml -Recurse
Write-Host $files.count "files present in source"
foreach ($file in $files) {
$s = Get-Content $file.PSPath
$s = $s -replace "string1", "replacement1") -replace "string2", "replacement2"
Set-Content $file.PSPath -Value $s
Write-Host $file.PSPath " modified"
}
foreach ($file in $files) {
$content = [System.IO.File]::ReadAllText($file).Replace("val1","val2")
[System.IO.File]::WriteAllText($file, $content)
Write-Host $file.PSPath " modified"
}
$files = Get-ChildItem "C:\temp" -Filter *.xml -Recurse -File
foreach ($file in $files)
{
(Get-Content $file.FullName) | Foreach-Object {
$_ -replace 'something1', 'something1aa' `
-replace 'something2', 'something2bb' `
-replace 'something3', 'something3cc' `
-replace 'something4', 'something4dd' `
-replace 'something5', 'something5dsf' `
-replace 'something6', 'something6dfsfds'
} | Set-Content $file.FullName
Write-Host $file.FullName " analysed"
}
尝试按如下方式修改代码:
$files = Get-ChildItem "source_folder" -Filter *.xml -Recurse
Write-Host $files.count "files present in source"
foreach ($file in $files) {
$s = Get-Content $file.PSPath
$s = $s -replace "string1", "replacement1") -replace "string2", "replacement2"
Set-Content $file.PSPath -Value $s
Write-Host $file.PSPath " modified"
}
foreach ($file in $files) {
$content = [System.IO.File]::ReadAllText($file).Replace("val1","val2")
[System.IO.File]::WriteAllText($file, $content)
Write-Host $file.PSPath " modified"
}
$files = Get-ChildItem "C:\temp" -Filter *.xml -Recurse -File
foreach ($file in $files)
{
(Get-Content $file.FullName) | Foreach-Object {
$_ -replace 'something1', 'something1aa' `
-replace 'something2', 'something2bb' `
-replace 'something3', 'something3cc' `
-replace 'something4', 'something4dd' `
-replace 'something5', 'something5dsf' `
-replace 'something6', 'something6dfsfds'
} | Set-Content $file.FullName
Write-Host $file.FullName " analysed"
}
尝试按如下方式修改代码:
$files = Get-ChildItem "source_folder" -Filter *.xml -Recurse
Write-Host $files.count "files present in source"
foreach ($file in $files) {
$s = Get-Content $file.PSPath
$s = $s -replace "string1", "replacement1") -replace "string2", "replacement2"
Set-Content $file.PSPath -Value $s
Write-Host $file.PSPath " modified"
}
foreach ($file in $files) {
$content = [System.IO.File]::ReadAllText($file).Replace("val1","val2")
[System.IO.File]::WriteAllText($file, $content)
Write-Host $file.PSPath " modified"
}
$files = Get-ChildItem "C:\temp" -Filter *.xml -Recurse -File
foreach ($file in $files)
{
(Get-Content $file.FullName) | Foreach-Object {
$_ -replace 'something1', 'something1aa' `
-replace 'something2', 'something2bb' `
-replace 'something3', 'something3cc' `
-replace 'something4', 'something4dd' `
-replace 'something5', 'something5dsf' `
-replace 'something6', 'something6dfsfds'
} | Set-Content $file.FullName
Write-Host $file.FullName " analysed"
}