简体   繁体   English

使用Powershell删除巨大的(300M)文本文件的顶行

[英]Remove top line of a huge (300M) text file with powershell

The difference between this question and Remove Top Line of Text File with Powershell is that my file is huge (over 300M). 这个问题和使用Powershell删除文本文件的顶行之间的区别是我的文件很大(超过300M)。

The technique I am using now is based on one of the answers to that question, however it seems inefficient for huge files. 我现在使用的技术基于该问题的答案之一 ,但是对于大型文件而言似乎效率不高。 Is there a faster (and less pretty) way to do it? 有没有更快(而且不太漂亮)的方法?

Try using a streamreader and streamwriter. 尝试使用streamreader和streamwriter。 This should perform quickly. 这应该很快执行。

$reader = [IO.File]::OpenText("C:\Users\Andy\Documents\input.txt")
$writer = New-Object System.IO.StreamWriter("C:\Users\Andy\Documents\output.txt")

$reader.ReadLine() > $null # Skip first line.
while ($reader.Peek() -ge 0) {
    $writer.writeline($reader.ReadLine())
}

$reader.Close()
$writer.Close()

I tested by creating a 300MB text file and used this on it. 我通过创建一个300MB的文本文件进行了测试,并在其上使用了该文件。 output.txt was created in 3.5 seconds :-). 在3.5秒内创建了output.txt :-)。

Update I optimized it slightly so it doesn't doesn't evaluate an expression every iteration of the loop. 更新我对它进行了稍微优化,以使它不会在循环的每次迭代中都不求值表达式。 Now it completes in 2.9 seconds :-) 现在完成了2.9秒:-)

If you don't mind memory bloat you can also do this. 如果您不介意内存膨胀,也可以这样做。 This is faster still but takes more memory, but less than Get-Content . 这仍然更快,但占用更多内存,但少于Get-Content

$reader = [IO.File]::OpenText("C:\Users\Andy\Documents\input.txt")
$writer = New-Object System.IO.StreamWriter("C:\Users\Andy\Documents\output.txt")

$reader.ReadLine() > $null # Skip first line.
$writer.write($reader.ReadToEnd())

$reader.Close()
$writer.Close()

As in Andy's answer, a StreamWriter is the way to go. 正如Andy的回答一样,StreamWriter是必经之路。 However, I saw better results from Measure-Command using gc $file -ReadCount 0 than a StreamReader. 但是,我看到使用gc $file -ReadCount 0 Measure-Command效果比StreamReader更好。

$contents=gc C:\My\File.txt -ReadCount 0
$w=New-Object System.IO.StreamWriter("C:\My\File.txt")
foreach($line in $contents){if(!$f++){continue}$w.WriteLine($line)}

This also has the added benefit of being able to read from and write to the same file. 这还具有能够读取和写入同一文件的额外好处。

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

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