我正在尝试获取一个巨大的文本文件的前几行(DB表的100K记录),并且它太大而无法在文本编辑器中进行操作。
如果我在linux中,我会使用head。我知道我可以使用
更多+99995 dbfile.txt
获取文件中的最后5行。是否有一个等效(和简单)的方法来获取FIRST线并像这样记录?
谢谢。
(请注意,必须记录此内容)。
答案 0 :(得分:0)
<强> head.bat 强>:
@echo off
setlocal DISABLEDELAYEDEXPANSION
set "NUM=%~1"
if "%NUM%" == "" set NUM=0
set LINE_INDEX=0
for /F "usebackq delims=" %%i in (`findstr /B /N /R /C:".*"`) do (
set LINE_STR=%%i
call :IF_OR_PRINT %%NUM%% NEQ 0 if %%LINE_INDEX%% GEQ %%NUM%% && exit /b 0
set /A LINE_INDEX+=1
)
exit /b 0
:IF_OR_PRINT
if %* exit /b 0
setlocal ENABLEDELAYEDEXPANSION
set OFFSET=0
:OFFSET_LOOP
set CHAR=!LINE_STR:~%OFFSET%,1!
if not "!CHAR!" == ":" ( set /A OFFSET+=1 && goto OFFSET_LOOP )
set /A OFFSET+=1
echo.!LINE_STR:~%OFFSET%!
exit /b 1
<强>使用强>:
输入文件| head.bat 1000
功能强>:
<强>问题强>:
答案 1 :(得分:-1)
为什么在Windows组中提到像Unix这样的玩具。你不聪明。
<强>剪切强>
filter cut {t|b} {i|x} NumOfLines
从文件的顶部或底部剪切行数。
t - top of the file
b - bottom of the file
i - include n lines
x - exclude n lines
示例强>
cscript //nologo filter cut t i 5 < "%systemroot%\win.ini"
脚本
Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
Set rs = CreateObject("ADODB.Recordset")
With rs
.Fields.Append "LineNumber", 4
.Fields.Append "Txt", 201, 5000
.Open
LineCount = 0
Do Until Inp.AtEndOfStream
LineCount = LineCount + 1
.AddNew
.Fields("LineNumber").value = LineCount
.Fields("Txt").value = Inp.readline
.UpDate
Loop
.Sort = "LineNumber ASC"
If LCase(Arg(1)) = "t" then
If LCase(Arg(2)) = "i" then
.filter = "LineNumber < " & LCase(Arg(3)) + 1
ElseIf LCase(Arg(2)) = "x" then
.filter = "LineNumber > " & LCase(Arg(3))
End If
ElseIf LCase(Arg(1)) = "b" then
If LCase(Arg(2)) = "i" then
.filter = "LineNumber > " & LineCount - LCase(Arg(3))
ElseIf LCase(Arg(2)) = "x" then
.filter = "LineNumber < " & LineCount - LCase(Arg(3)) + 1
End If
End If
Do While not .EOF
Outp.writeline .Fields("Txt").Value
.MoveNext
Loop
End With