我有一个大小为2.5 GB的日志文件。有没有办法使用Windows命令提示符将该文件分割成更小的文件?
split
命令来分割文件:
将文件拆分成每个500MB的大小:split myLargeFile.txt -b 500m
将文件拆分成每个10000行的大小:split myLargeFile.txt -l 10000
如果你没有 Git/Git Bash,请在https://git-scm.com/download下载。
如果你丢失了 Git Bash 的快捷方式,则可以使用C:\Program Files\Git\git-bash.exe
运行它。
我总是喜欢例子...
示例:
你可以看到这张图片中 split
生成的文件名为 xaa
,xab
,xac
等。
这些名称由前缀和后缀组成,您可以指定它们的样式。由于我没有指定前缀或后缀的外观,因此前缀默认为x
,后缀默认为两个字母的字母枚举。
另一个例子:
此示例演示:
MySlice
(而不是默认值x
),-d
标志(而不是aa
,ab
,ac
等)-a 5
告诉它我希望后缀长度为5位数:split
部分,应该在你的主要答案中使用_filename prefix_和-d
选项。相关链接:https://dev59.com/El8e5IYBdhLWcg3w6NyW#45761990 - affsplit *.txt MyNewText -b 5m
- Just Mesplit myLargeFile.txt slice_ -l 1000
然后执行 cat slice_* > combined.txt
。 - Josh Withee您必须安装Git Bash,并在该终端/ shell中工作。
您可以使用命令split来完成此任务。 例如,将此命令输入到命令提示符中:
split YourLogFile.txt -b 500m
创建大小为500 MByte的多个文件。对于您的文件大小,这将需要几分钟时间。您可以将输出文件(默认称为“xaa”,“xab”等)重命名为*.txt,在您选择的编辑器中打开它。
确保查看命令的帮助文件。您还可以将日志文件按行数拆分或更改输出文件的名称。
已在以下系统进行测试:
以下代码将文件分割为每500个
@echo off
setlocal ENABLEDELAYEDEXPANSION
REM Edit this value to change the name of the file that needs splitting. Include the extension.
SET BFN=upload.txt
REM Edit this value to change the number of lines per file.
SET LPF=15000
REM Edit this value to change the name of each short file. It will be followed by a number indicating where it is in the list.
SET SFN=SplitFile
REM Do not change beyond this line.
SET SFX=%BFN:~-3%
SET /A LineNum=0
SET /A FileNum=1
For /F "delims==" %%l in (%BFN%) Do (
SET /A LineNum+=1
echo %%l >> %SFN%!FileNum!.%SFX%
if !LineNum! EQU !LPF! (
SET /A LineNum=0
SET /A FileNum+=1
)
)
endlocal
Pause
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
剪切
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.vbs cut t i 5 < "%systemroot%\win.ini"
另一种方法是输出5001行及以上的内容,根据您的需要进行调整。此方法几乎不消耗内存。
Do Until Inp.AtEndOfStream
Count = Count + 1
If count > 5000 then
OutP.WriteLine Inp.Readline
End If
Loop
c:\windows\sysnative\cscript
等 - sysnative强制运行System32文件而不是SysWoW64文件,适用于32位进程)。如果是32位,我们需要另一种特定的技术,而不是通用的。 - billSplit text file (max lines each):
: Initialize
set input=file.txt
set max=10000
set /a line=1 >nul
set /a file=1 >nul
set out=!file!_%input%
set /a max+=1 >nul
echo Number of lines in %input%:
find /c /v "" < %input%
: Split file
for /f "tokens=* delims=[" %i in ('type "%input%" ^| find /v /n ""') do (
if !line!==%max% (
set /a line=1 >nul
set /a file+=1 >nul
set out=!file!_%input%
echo Writing file: !out!
)
REM Write next file
set a=%i
set a=!a:*]=]!
echo:!a:~1!>>out!
set /a line+=1 >nul
)
list1.txt
这段内容与编程有关。\[[0-9]\]
\[[0-9][0-9]\]
\[[0-9][0-9][0-9]\]
\[[0-2][0-9][0-9][0-9]\]
list2.txt
`\[[3-5][0-9][0-9][0-9]\]` (请注意转义字符)list3.txt
\[[6-9][0-9][0-9][0-9]\]
type "%input%" | find /v /n "" | findstr /b /r /g:list1.txt > file1.txt type "%input%" | find /v /n "" | findstr /b /r /g:list2.txt > file2.txt type "%input%" | find /v /n "" | findstr /b /r /g:list3.txt > file3.txt
for /f "tokens=* delims=[" %i in ('type "%cd%\file1.txt"') do ( set a=%i set a=!a:*]=]! echo:!a:~1!>>file_1.txt)
注:
适用于前导空格、空行和空白行。
在 Win 10 x64 CMD 上测试通过,适用于 4.4GB 的文本文件,5651982 行。