批量拆分文本文件 [英] Batch split a text file

查看:254
本文介绍了批量拆分文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个批处理档案来分割txt档案:

  @echo off 
for / ftokens = 1 * delims =:%% a in('findstr / n^PASSWORD.txt')do for / fdelims =〜%% c in(%%〜b)do& text %% a.txtecho(%% c
pause

提前感谢。



编辑:

 我刚刚尝试过:

@echo off
setlocal ENABLEDELAYEDEXPANSION
REM编辑此值以更改文件,包括扩展名
SET BFN = passwordAll.txt
REM编辑此值以更改每个文件的行数
SET LPF = 50000
REM这个值改变每个短文件的名称,后面跟一个数字,表示它在列表中的位置
SET SFN = SplitFile

REM不要改变这一行。

SET SFX =%BFN:〜-3%

SET / A LineNum = 0
SET / A FileNum = 1

对于/ Fdelims ==%% 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
暂停
退出

但我得到一个错误说:没有足够的存储可用来处理命令

解决方案

这将给你一个基本的骨架。根据需要修改

  @echo off 
setlocal enableextensions disabledelayedexpansion

setnLines = 5000
setline = 0

for / fusebackq delims =%% a in(passwords.txt)do(
set / afile = line /%nLines%,line + = 1
setlocal enabledelayedexpansion
for(!file!)do(
endlocal
> %b.txtecho(%% a



endlocal

$ b b

已编辑



正如评论指出的,4.3GB文件难以管理。 for / f 需要将整个文件加载到内存中,所需的缓冲区大小是文件在内存中转换为unicode时的两倍。



这是一个完全临时的解决方案,我没有测试它在一个高的文件,但至少在理论上它应该工作(除非5000行需要大量的内存,这取决于行的长度) p>

AND,对于这样的文件,它将是

  @echo off 
setlocal enableextensions disabledelayedexpansion

setline = 0
settempFile =%temp%\passwords.tmp

findstr / n ^passwords.txt> %tempFile%
for / f %% a in('type passwords.txt ^ | find / c / v')do set / anFiles = %% a / 5000

for / l %% a in(0 1%nFiles%)do(
set / ae1 = %% a * 5,e2 = e1 + 1,e3 = e2 + 1,e4 = e3 + 1,e5 = e4 + 1
setlocal enabledelayedexpansion
如果%% a equ 0(
设置e = / c: 9]:/ c:[1-9] [0-9]:/ c:[1-9] [0-9] [0-9]:/ c:!e2![0 -9] [0-9] [0-9]:/ c:!e3![0-9] [0-9] [0-9]:/ c: ] [0-9] [0-9]:/ c:!e5![0-9] [0-9] [0-9]:
)else e = / c:!e1![0-9] [0-9] [0-9]:/ c:!e2![0-9] [0-9] / c:!e3![0-9] [0-9] [0-9]:/ c:!e4![0-9] [0-9] c:!e5![0-9] [0-9] [0-9]:

for / fdelims =%% e in(!e! )do(
endlocal&(for / ftokens = 1,* delims =:%% b in('findstr / r / b %% e'%tempFile%')do @echo %c)>密码_ %% a.txt



del%tempFile%> nul 2> nul

endlocal

已修改:以前的代码无法正常工作冒号,因为它已在中用作命令的分隔符以从数据中分隔行号。



对于另一种选择,仍然是纯批次但仍然是SLOW

  @ echo off 
setlocal enableextensions disableelayedexpansion

setnLines = 5000
setline = 0
for / f %% a in('type passwords.txt ^ | find / c / v')do setfileLines = %% a

< passwords.txt(对于/ l %% a in(11%fileLines%)do(
set / pdata =
set / afile = line /%nLines% line + = 1
setlocal enabledelayedexpansion
>>passwords_!file!.txtecho(!data!
endlocal
))
$ b b endlocal


I have this batch file to split a txt file:

@echo off
for /f "tokens=1*delims=:" %%a in ('findstr /n "^" "PASSWORD.txt"') do for /f "delims=~" %%c in ("%%~b") do >"text%%a.txt" echo(%%c
pause

It works but it splits it line by line. How do i make it split it every 5000 lines. Thanks in advance.

Edit:

I have just tried this:

@echo off
setlocal ENABLEDELAYEDEXPANSION
REM Edit this value to change the name of the file that needs splitting. Include the extension.
SET BFN=passwordAll.txt
REM Edit this value to change the number of lines per file.
SET LPF=50000
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
exit

But i get an error saying: Not enough storage is available to process this command

解决方案

This will give you the a basic skeleton. Adapt as needed

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "nLines=5000"
    set "line=0"

    for /f "usebackq delims=" %%a in ("passwords.txt") do (
        set /a "file=line/%nLines%", "line+=1"
        setlocal enabledelayedexpansion
        for %%b in (!file!) do (
            endlocal
            >>"passwords_%%b.txt" echo(%%a
        )
    )

    endlocal

EDITED

As the comments indicated, a 4.3GB file is hard to manage. for /f needs to load the full file into memory, and the buffer needed is twice this size as the file is converted to unicode in memory.

This is a fully ad hoc solution. I've not tested it over a file that high, but at least in theory it should work (unless 5000 lines needs a lot of memory, it depends of the line length)

AND, with such a file it will be SLOW

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "line=0"
    set "tempFile=%temp%\passwords.tmp"

    findstr /n "^" passwords.txt > "%tempFile%"
    for /f %%a in ('type passwords.txt ^| find /c /v "" ') do set /a "nFiles=%%a/5000"

    for /l %%a in (0 1 %nFiles%) do (
        set /a "e1=%%a*5", "e2=e1+1", "e3=e2+1", "e4=e3+1", "e5=e4+1"
        setlocal enabledelayedexpansion
        if %%a equ 0 (
            set "e=/c:"[1-9]:" /c:"[1-9][0-9]:" /c:"[1-9][0-9][0-9]:" /c:"!e2![0-9][0-9][0-9]:" /c:"!e3![0-9][0-9][0-9]:" /c:"!e4![0-9][0-9][0-9]:" /c:"!e5![0-9][0-9][0-9]:" "
        ) else (
            set "e=/c:"!e1![0-9][0-9][0-9]:" /c:"!e2![0-9][0-9][0-9]:" /c:"!e3![0-9][0-9][0-9]:" /c:"!e4![0-9][0-9][0-9]:" /c:"!e5![0-9][0-9][0-9]:" "
        )
        for /f "delims=" %%e in ("!e!") do (
            endlocal & (for /f "tokens=1,* delims=:" %%b in ('findstr /r /b %%e "%tempFile%"') do @echo(%%c)>passwords_%%a.txt
        )
    )

    del "%tempFile%" >nul 2>nul

    endlocal

EDITED, again: Previous code will not correctly work for lines starting with a colon, as it has been used as a delimiter in the for command to separate line numbers from data.

For an alternative, still pure batch but still SLOW

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "nLines=5000"
    set "line=0"
    for /f %%a in ('type passwords.txt^|find /c /v ""') do set "fileLines=%%a"

    < "passwords.txt" (for /l %%a in (1 1 %fileLines%) do (
        set /p "data="
        set /a "file=line/%nLines%", "line+=1"
        setlocal enabledelayedexpansion
        >>"passwords_!file!.txt" echo(!data!
        endlocal
    ))

    endlocal

这篇关于批量拆分文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆