큰 텍스트 파일에서 행을 임의로 섞습니다. 텍스트 파일이 있으며 행을 임의로 셔플해야합니다. 가능합니까?

약 6k 행 (각 행이 매우 길다)의 ~ 1GB의 텍스트 파일이 있으며 행을 임의로 셔플해야합니다. 가능합니까? 아마도 awk와 함께?



답변

GNU coreutils 에서 shuf명령을 사용할 수 있습니다 . 이 유틸리티는 매우 빠르며 1GB 파일을 섞는 데 1 분도 걸리지 않습니다.

아래 명령 shuf은 출력 파일을 열기 전에 전체 입력을 읽으 므로 귀하의 경우에는 효과가 있습니다 .

$ shuf -o File.txt < File.txt


답변

파이썬 원 라이너 :

python -c 'import sys, random; L = sys.stdin.readlines(); random.shuffle(L); print "".join(L),'

표준 입력에서 모든 줄을 읽고 그 자리에서 섞은 다음 줄 바꿈 문자를 추가하지 않고 인쇄합니다 ( ,끝에서 알 수 있음 ).


답변

OSX의 경우 이진을이라고 gshuf합니다.

brew install coreutils
gshuf -o File.txt < File.txt


답변

나처럼 당신이 여기 shufmacOS 에 대한 대안을 찾기 위해 온다면 을 사용하십시오 randomize-lines.

기능이 유사한 명령 randomize-lines이있는 (homebrew) 패키지를 설치하십시오 .rlshuf

brew install randomize-lines

Usage: rl [OPTION]... [FILE]...
Randomize the lines of a file (or stdin).

  -c, --count=N  select N lines from the file
  -r, --reselect lines may be selected multiple times
  -o, --output=FILE
                 send output to file
  -d, --delimiter=DELIM
                 specify line delimiter (one character)
  -0, --null     set line delimiter to null character
                 (useful with find -print0)
  -n, --line-number
                 print line number with output lines
  -q, --quiet, --silent
                 do not output any errors or warnings
  -h, --help     display this help and exit
  -V, --version  output version information and exit


답변

나는 이것을 발견 한 것을 잊었지만 여기 shuffle.pl에 내가 사용하는 것이 있습니다 :

#!/usr/bin/perl -w

# @(#) randomize Effectively _unsort_ a text file into random order.
# 96.02.26 / drl.
# Based on Programming Perl, p 245, "Selecting random element ..."

# Set the random seed, PP, p 188
srand(time|$$);

# Suck in everything in the file.
@a = <>;

# Get random lines, write 'em out, mark 'em done.
while ( @a ) {
        $choice = splice(@a, rand @a, 1);
        print $choice;
}


답변

적어도 우분투에는 다음과 같은 프로그램이 있습니다. shuf

shuf file.txt


답변