在Bash脚本中,我想从输入文件中随机挑选N行并输出到另一个文件。

如何做到这一点呢?


当前回答

使用shuf和-n选项,如下所示,得到N个随机行:

shuf -n N input > output

其他回答

seq 1 100 | python3 -c 'print(__import__("random").choice(__import__("sys").stdin.readlines()))'

随机排序文件,并选择前100行:

lines=100
input_file=/usr/share/dict/words

# This is the basic selection method
<$input_file sort -R | head -n $lines

# If the file has duplicates that must never cause duplicate results
<$input_file sort | uniq        | sort -R | head -n $lines

# If the file has blank lines that must be filtered, use sed
<$input_file sed $'/^[ \t]*$/d' | sort -R | head -n $lines

当然<$input_file可以用任何管道标准输入替换。This (sort -R and $'…\t…'来让sed匹配制表符)适用于GNU/Linux和BSD/macOS。

我的首选是非常快的,我采样了一个以制表符分隔的数据文件,它有13列,23.1万行,2.0GB未压缩。

# randomly sample select 5% of lines in file
# including header row, exclude blank lines, new seed

time \
awk 'BEGIN  {srand()} 
     !/^$/  { if (rand() <= .05 || FNR==1) print > "data-sample.txt"}' data.txt

# awk  tsv004  3.76s user 1.46s system 91% cpu 5.716 total

使用shuf和-n选项,如下所示,得到N个随机行:

shuf -n N input > output

下面的“c”是要从输入中选择的行数。根据需要修改:

#!/bin/sh

gawk '
BEGIN   { srand(); c = 5 }
c/NR >= rand() { lines[x++ % c] = $0 }
END { for (i in lines)  print lines[i] }

' "$@"