标题: [文本处理] 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
- <#*,:&cls
- @echo off
- cd /d "%~dp0"
- powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
- pause
- exit /b
- #>
- # 功能:删除文件的最后N行
- # 大文件
- $file = "largeFile.txt"
- # 最后N行
- $lastN = 7
- # 换行分割符
- $charDelims = 13
-
- try {
- $stream1 = New-Object System.IO.FileStream -ArgumentList @($file, [System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None)
- $pos = $stream1.Seek(0, [System.IO.SeekOrigin]::End)
- $ctrLines = 0
- while ($ctrLines -lt $lastN -and $pos -gt 0) {
- $pos = $stream1.Seek(-1, [System.IO.SeekOrigin]::Current)
- $byte = $stream1.ReadByte()
- if ($byte -eq $charDelims) {
- $ctrLines++
- }
- $pos = $stream1.Seek(-1, [System.IO.SeekOrigin]::Current)
- }
- $stream1.SetLength($pos)
- } catch {
- $_ | Write-Host -ForegroundColor Red
- } finally {
- if ($stream1) {
- $stream1.Dispose()
- }
- }
复制代码
作者: for_flr 时间: 2021-12-13 10:12
- #&@cls&powershell "gc %~0|out-string|iex"&pause&exit
- $file="aa.txt"
- $txt=gc $file -readcount 0
- $txt7=$txt[0..($txt.length-1-7)]
- sc -value $txt7 "new-$file"
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |