如何在Windows中拆分大型文本文件?

148

我有一个大小为2.5 GB的日志文件。有没有办法使用Windows命令提示符将该文件分割成更小的文件?


可能 [重复] (https://dev59.com/Pn_aa4cB1Zd3GeqP45Th) - Stephan
https://github.com/Mitch-Wheat/FileSplitter - Mitch Wheat
5个回答

383
如果你已经安装了 Git for Windows,那么你应该已经安装了 Git Bash,因为它是 Git 的一部分。
在 Git Bash 中使用 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运行它。

就这些!


我总是喜欢例子...

示例:

enter image description here

你可以看到这张图片中 split 生成的文件名为 xaaxabxac 等。

这些名称由前缀和后缀组成,您可以指定它们的样式。由于我没有指定前缀或后缀的外观,因此前缀默认为x,后缀默认为两个字母的字母枚举。

另一个例子:

此示例演示:

  • 使用文件名前缀 MySlice(而不是默认值x),
  • 使用数字后缀的-d标志(而不是aaabac等)
  • 选项-a 5告诉它我希望后缀长度为5位数:

输入图像描述


4
不错的解决方案,附有良好的示例。在Windows 10中对我很有效。我使用了“split myLogs.log mylogs_ -b 800m -a 3 -d”将一个4.5GB的日志文件进行拆分。 - user3437460
1
在Windows上使用Git-bash的好方法。对于split部分,应该在你的主要答案中使用_filename prefix_和-d选项。相关链接:https://dev59.com/El8e5IYBdhLWcg3w6NyW#45761990 - aff
好的,但是如果我想在Git Bash中选择*.txt文件,为什么不起作用呢?例如:split *.txt MyNewText -b 5m - Just Me
18
不错!您还可以在输出上设置扩展名...例如:--additional-suffix=.txt - Nij
1
@AlbertChen 使用 split myLargeFile.txt slice_ -l 1000 然后执行 cat slice_* > combined.txt - Josh Withee
显示剩余11条评论

3

您必须安装Git Bash,并在该终端/ shell中工作。

您可以使用命令split来完成此任务。 例如,将此命令输入到命令提示符中:

split YourLogFile.txt -b 500m

创建大小为500 MByte的多个文件。对于您的文件大小,这将需要几分钟时间。您可以将输出文件(默认称为“xaa”,“xab”等)重命名为*.txt,在您选择的编辑器中打开它。

确保查看命令的帮助文件。您还可以将日志文件按行数拆分或更改输出文件的名称。

已在以下系统进行测试:

  • Windows 7 64位
  • Windows 10 64位

31
“'split' 不被识别为内部或外部命令,可执行程序或批处理文件。”- 在 Win 7 Ultimate SP1, 64 位操作系统。 - Christopher J Smith
1
我相信你实际上使用了Git Bash的split命令。 - Eljah
好的,但是如果我想在Git Bash中选择*.txt文件,为什么不起作用?例如:split *.txt MyNewText -b 5m - Just Me
我在Windows 10上尝试了GNU bash,版本为4.4.23(1)-release,它按照广告所说的那样工作。 - mico
它在 PyCharm IDE 内的 "Git Bash" 终端中,Windows 10 主机上运行得非常好。谢谢! - Nikolai Varankine
-b 500m 在 Windows 上给了我大量的小文件(我想是 500 字节)。我使用了 -n 4。 - Pavel

3

以下代码将文件分割为每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

请看下面:https://forums.techguy.org/threads/solved-split-a-100000-line-csv-into-5000-line-csv-files-with-dos-batch.1023949/

2
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

当我尝试运行脚本时,出现了错误,如filter.vbs(16, 13) Microsoft Cursor Engine: Out of memory。 - Albin
你的Windows是32位还是64位?如果是64位,请从64位版本运行它(c:\windows\sysnative\cscript等 - sysnative强制运行System32文件而不是SysWoW64文件,适用于32位进程)。如果是32位,我们需要另一种特定的技术,而不是通用的。 - bill
我有一个 64 位的 Windows。 - Albin
所以我告诉你在那种情况下该怎么做了。Sysnative 强制使用 64 位。 - bill

1
当然有!Win CMD 可以做的不仅仅是拆分文本文件 :)
将文本文件拆分为每个文件包含“max”行:
Split 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
)

如果上面的代码挂起或崩溃,这个示例代码通过将数据写入中间文件而不是将所有内容保存在内存中来更快地拆分文件:
例如,将具有7,600行的文件拆分为最多3000行的较小文件。
1. 使用“set”命令生成正则表达式字符串/模式文件,以供馈送到“findstr”的“/g”标志。

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]\]

  1. 将文件分割成较小的文件:
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
  1. 移除每个文件分割的前缀行号:
    例如,对于第一个文件:
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 行。


我可以在哪里使用这段代码?如何使用它? - Just Me
测试是在Win 10 x64命令行上进行的,针对一个4.4GB的文本文件。 - Zimba

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接