How to split one text file into multiple *.txt files?

前端 未结 9 598
[愿得一人]
[愿得一人] 2020-12-07 17:39

I got a text file file.txt(12 MBs) containing:

something1
something2
something3
something4
(...)

Is there any way to split

9条回答
  •  有刺的猬
    2020-12-07 18:05

    Using bash:

    readarray -t LINES < file.txt
    COUNT=${#LINES[@]}
    for I in "${!LINES[@]}"; do
        INDEX=$(( (I * 12 - 1) / COUNT + 1 ))
        echo "${LINES[I]}" >> "file${INDEX}.txt"
    done
    

    Using awk:

    awk '{
        a[NR] = $0
    }
    END {
        for (i = 1; i in a; ++i) {
            x = (i * 12 - 1) / NR + 1
            sub(/\..*$/, "", x)
            print a[i] > "file" x ".txt"
        }
    }' file.txt
    

    Unlike split this one makes sure that number of lines are most even.

提交回复
热议问题