[英]How to find and replace within a large binary file with PowerShell?
I have binary files larger than 50 GB, which contain a specific string I want to replace with equal length all-spaces string. 我有大于50 GB的二进制文件,其中包含一个特定的字符串,我想用等长的全空格字符串替换。 The string I am looking for is in the beginning of file, say within the first megabyte. 我正在寻找的字符串是在文件的开头,比如在第一兆字节内。 How can I do this with PowerShell? 如何使用PowerShell执行此操作?
I am afraid [System.IO.File]::ReadAllBytes("myfile.bin")
is not the solution, because I don't want to load the whole binary. 我担心[System.IO.File]::ReadAllBytes("myfile.bin")
不是解决方案,因为我不想加载整个二进制文件。 I want to search and replace within the first megabyte. 我想在第一兆字节内搜索和替换。
Adopted from C#, so some refactoring might be needed: 从C#采用,因此可能需要进行一些重构:
$path = "\path\to\binary\file"
$numberOfBytesToRead = 1000000
$stringToSearch = "Hello World!"
$enc = [system.Text.Encoding]::UTF8
[Byte[]]$replacementString = $enc.GetBytes(" ");
$fileStream = [System.IO.File]::Open($path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite)
# binary reader to search for the string
$binaryReader = New-Object System.IO.BinaryReader($fileStream)
# get the contents of the beginning of the file
[Byte[]] $byteArray = $binaryReader.ReadBytes($numberOfBytesToRead)
# look for string
$m = [Regex]::Match([Text.Encoding]::ASCII.GetString($byteArray), $stringToSearch)
if ($m.Success)
{
echo "Found '$stringToSearch' at position "$m.Index
}
else
{
echo "'$stringToSearch' was not found"
}
$fileStream.Close()
# reopen to write
$fileStream = [System.IO.File]::Open($path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Write, [System.IO.FileShare]::ReadWrite)
$binaryWriter = New-Object System.IO.BinaryWriter($fileStream)
# set file position to location of the string
$binaryWriter.BaseStream.Position = $m.Index;
$binaryWriter.Write($replacementString)
$fileStream.Close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.