Split text file into smaller multiple text file using command line

后端 未结 9 2080
借酒劲吻你
借酒劲吻你 2020-12-22 18:03

I have multiple text file with about 100,000 lines and I want to split them into smaller text files of 5000 lines each.

I used:

split -l 5000 filena         


        
9条回答
  •  隐瞒了意图╮
    2020-12-22 18:23

    @ECHO OFF
    SETLOCAL
    SET "sourcedir=U:\sourcedir"
    SET /a fcount=100
    SET /a llimit=5000
    SET /a lcount=%llimit%
    FOR /f "usebackqdelims=" %%a IN ("%sourcedir%\q25249516.txt") DO (
     CALL :select
     FOR /f "tokens=1*delims==" %%b IN ('set dfile') DO IF /i "%%b"=="dfile" >>"%%c" ECHO(%%a
    )
    GOTO :EOF
    :select
    SET /a lcount+=1
    IF %lcount% lss %llimit% GOTO :EOF
    SET /a lcount=0
    SET /a fcount+=1
    SET "dfile=%sourcedir%\file%fcount:~-2%.txt"
    GOTO :EOF
    

    Here's a native windows batch that should accomplish the task.

    Now I'll not say that it'll be fast (less than 2 minutes for each 5Kline output file) or that it will be immune to batch character-sensitivites. Really depends on the characteristics of your target data.

    I used a file named q25249516.txt containing 100Klines of data for my testing.


    Revised quicker version

    REM

    @ECHO OFF
    SETLOCAL
    SET "sourcedir=U:\sourcedir"
    SET /a fcount=199
    SET /a llimit=5000
    SET /a lcount=%llimit%
    FOR /f "usebackqdelims=" %%a IN ("%sourcedir%\q25249516.txt") DO (
     CALL :select
     >>"%sourcedir%\file$$.txt" ECHO(%%a
    )
    SET /a lcount=%llimit%
    :select
    SET /a lcount+=1
    IF %lcount% lss %llimit% GOTO :EOF
    SET /a lcount=0
    SET /a fcount+=1
    MOVE /y "%sourcedir%\file$$.txt" "%sourcedir%\file%fcount:~-2%.txt" >NUL 2>nul
    GOTO :EOF
    

    Note that I used llimit of 50000 for testing. Will overwrite the early file numbers if llimit*100 is gearter than the number of lines in the file (cure by setting fcount to 1999 and use ~3 in place of ~2 in file-renaming line.)

提交回复
热议问题