How delete a line having a word quickly in multiple large files using powershell

回眸只為那壹抹淺笑 提交于 2020-12-26 03:45:14

问题


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

回答1:


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
    }
  )
}

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.


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'
  )
}

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).



来源:https://stackoverflow.com/questions/59758144/how-delete-a-line-having-a-word-quickly-in-multiple-large-files-using-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!