[bash] 큰 텍스트 파일을 같은 줄 수의 작은 파일로 나누는 방법은 무엇입니까?

큰 줄 수의 일반 텍스트 파일을 가지고 있으며 작은 줄로 나눠서 작은 파일로 나눕니다. 따라서 파일에 약 2M 줄이 있으면 200k 줄이 포함 된 10 개의 파일 또는 20k 줄이 포함 된 100 개의 파일로 분할하고 싶습니다 (나머지가있는 하나의 파일; 고르게 나눌 수는 중요하지 않음).

파이썬에서 상당히 쉽게 할 수 있지만 bash와 유닉스 유틸리티를 사용하여 닌자 방법이 있는지 궁금합니다 (수동으로 루핑하고 계산 / 분할하는 것이 아니라).



답변

split 명령을 보셨습니까?

$ split --help
Usage: split [OPTION] [INPUT [PREFIX]]
Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default
size is 1000 lines, and default PREFIX is `x'.  With no INPUT, or when INPUT
is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -a, --suffix-length=N   use suffixes of length N (default 2)
  -b, --bytes=SIZE        put SIZE bytes per output file
  -C, --line-bytes=SIZE   put at most SIZE bytes of lines per output file
  -d, --numeric-suffixes  use numeric suffixes instead of alphabetic
  -l, --lines=NUMBER      put NUMBER lines per output file
      --verbose           print a diagnostic to standard error just
                            before each output file is opened
      --help     display this help and exit
      --version  output version information and exit

다음과 같이 할 수 있습니다.

split -l 200000 filename

각각 200000 줄의 파일을 생성합니다 xaa xab xac

출력 파일의 크기로 분할되는 또 다른 옵션 (여전히 줄 바꿈으로 분할) :

 split -C 20m --numeric-suffixes input_filename output_prefix

output_prefix01 output_prefix02 output_prefix03 ...최대 크기가 각각 20MB 인 파일을 만듭니다 .


답변

방법에 대한 분할 명령?

split -l 200000 mybigfile.txt


답변

예, split명령이 있습니다. 파일을 줄이나 바이트로 나눕니다.

$ split --help
Usage: split [OPTION]... [INPUT [PREFIX]]
Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default
size is 1000 lines, and default PREFIX is `x'.  With no INPUT, or when INPUT
is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -a, --suffix-length=N   use suffixes of length N (default 2)
  -b, --bytes=SIZE        put SIZE bytes per output file
  -C, --line-bytes=SIZE   put at most SIZE bytes of lines per output file
  -d, --numeric-suffixes  use numeric suffixes instead of alphabetic
  -l, --lines=NUMBER      put NUMBER lines per output file
      --verbose           print a diagnostic just before each
                            output file is opened
      --help     display this help and exit
      --version  output version information and exit

SIZE may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.


답변

사용하다 split

파일을 고정 크기 조각으로 분할하고 INPUT의 연속 섹션을 포함하는 출력 파일을 작성합니다 (제공되지 않거나 INPUT이`- ‘인 경우 표준 입력)

Syntax
split [options] [INPUT [PREFIX]]

http://ss64.com/bash/split.html


답변

사용하다:

sed -n '1,100p' filename > output.txt

여기서 1과 100은에서 캡처 할 줄 번호입니다 output.txt.


답변

“file.txt”파일을 10000 라인 파일로 분할하십시오.

split -l 10000 file.txt


답변

split(GNU coreutils, 2010-12-22 버전 8.8 이후 )에는 다음 매개 변수가 포함됩니다.

-n, --number=CHUNKS     generate CHUNKS output files; see explanation below

CHUNKS may be:
  N       split into N files based on size of input
  K/N     output Kth of N to stdout
  l/N     split into N files without splitting lines/records
  l/K/N   output Kth of N to stdout without splitting lines/records
  r/N     like 'l' but use round robin distribution
  r/K/N   likewise but only output Kth of N to stdout

따라서 split -n 4 input output.네 개의 파일을 생성합니다 (output.a{a,b,c,d} 같은 양의 바이트로 )을 하지만 중간에 행이 끊어 질 수 있습니다.

전체 줄을 유지하려면 (즉, 줄로 나누기) 다음과 같이 작동합니다.

split -n l/4 input output.

관련 답변 : https://stackoverflow.com/a/19031247