Board logo

标题: [文本处理] bat如何删除txt最后的7行 [打印本页]

作者: mio    时间: 2021-12-12 11:20     标题: bat如何删除txt最后的7行


aa
bb
c
d
e
f
g
h
变为
aa

这个怎么写?求大神解答谢谢
作者: mio    时间: 2021-12-12 11:30

文本约有10mb,30w行
作者: flashercs    时间: 2021-12-12 13:58

  1. <#*,:&cls
  2. @echo off
  3. cd /d "%~dp0"
  4. powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
  5. pause
  6. exit /b
  7. #>
  8. # 功能:删除文件的最后N行
  9. # 大文件
  10. $file = "largeFile.txt"
  11. # 最后N行
  12. $lastN = 7
  13. # 换行分割符
  14. $charDelims = 13
  15. try {
  16.   $stream1 = New-Object System.IO.FileStream -ArgumentList @($file, [System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
  17.   $pos = $stream1.Seek(0, [System.IO.SeekOrigin]::End)
  18.   $ctrLines = 0
  19.   while ($ctrLines -lt $lastN -and $pos -gt 0) {
  20.     $pos = $stream1.Seek(-1, [System.IO.SeekOrigin]::Current)
  21.     $byte = $stream1.ReadByte()
  22.     if ($byte -eq $charDelims) {
  23.       $ctrLines++
  24.     }
  25.     $pos = $stream1.Seek(-1, [System.IO.SeekOrigin]::Current)
  26.   }
  27.   $stream1.SetLength($pos)
  28. } catch {
  29.   $_ | Write-Host -ForegroundColor Red
  30. } finally {
  31.   if ($stream1) {
  32.     $stream1.Dispose()
  33.   }
  34. }
复制代码

作者: for_flr    时间: 2021-12-13 10:12

  1. #&@cls&powershell "gc %~0|out-string|iex"&pause&exit
  2. $file="aa.txt"
  3. $txt=gc $file -readcount 0
  4. $txt7=$txt[0..($txt.length-1-7)]
  5. sc -value $txt7 "new-$file"
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2