如何使用powershell在多个大文件中快速删除包含单词的行 [英] How delete a line having a word quickly in multiple large files using powershell
问题描述
如何使用PowerShell在多个大文件中快速删除有单词的一行
How delete a line having a word quickly in multiple large files using PowerShell
我正在使用以下代码,但需要很长时间
i am using the below code but it take long time
$files = Get-ChildItem "D:\mjautomation\v19.0\filesdd\"
foreach ($file in $files) {
$c = Get-Content $file.fullname | where { $_ -notmatch "deletethisline" }
$c | Set-Content $file.fullname
推荐答案
由于使用了 switch -File
,但请注意,它需要将每个文件作为一个整体读入内存em>(减去排除的行):
The following should be reasonably fast due to use of switch -File
, but note that it requires reading each file into memory as a whole (minus the excluded lines):
foreach ($file in Get-ChildItem -File D:\mjautomation\v19.0\filesdd) {
Set-Content $file.FullName -Value $(
switch -Regex -File $file.FullName {
'deletethisline' {} # ignore
default { $_ } # pass line through
}
)
}
如果您不想(几乎)完整地将每个文件读入内存,请使用 [System.IO.StreamWriter]
实例,如图这个答案 而不是 Set-Content
写入一个临时文件,然后替换原来的文件.
If you don't want to read each file into memory in (almost) full, use a [System.IO.StreamWriter]
instance, as shown in this answer instead of Set-Content
to write to a temporary file, and then replace the original file.
这样做还有一个额外的好处,那就是避免了通过内存操作写回原始文件所带来的数据丢失的小风险.
Doing so has the added advantage of avoiding the small risk of data loss that writing back to the original file via in-memory operations bears.
如果你想凑合 - 较慢 - Get-Content
cmdlet,使用如下;与上述相同的警告适用:
If you want to make do with the - slower - Get-Content
cmdlet, use the following; the same caveats as above apply:
foreach ($file in Get-ChildItem -File D:\mjautomation\v19.0\filesdd) {
Set-Content $file.FullName -Value (
@(Get-Content $file.FullName) -notmatch 'deletethisline'
)
}
请注意,作为 foreach
loop 的替代方案,您可以使用带有 ForEach-Object
cmdlet -Get-ChildItem ... |ForEach-Object { <# work with $_ #>}
- 但这样做会比较慢(尽管在很多情况下这无关紧要).
Note that as an alternative to the foreach
loop you can use a single pipeline with the ForEach-Object
cmdlet - Get-ChildItem ... | ForEach-Object { <# work with $_ #> }
- but doing so is slower (though in many cases that won't matter).
这篇关于如何使用powershell在多个大文件中快速删除包含单词的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!