0

我有一个 256MB 的文件。我想从此文件中检索随机数量的数据并将其复制到另一个文件中。

有没有办法在 bash 或其他方式中做到这一点?

编辑:选择 1 到 256 之间的随机数,然后将该数量的 mb 从一个文件复制到另一个文件。

4

3 回答 3

4

这从头开始复制:

# (random) blocks of one byte
dd of=output_file if=input_file ibs=1 count=$((($RANDOM % 256) + 1)M

# one block of (random) bytes
dd of=output_file if=input_file ibs=$((($RANDOM % 256) + 1)M count=1

如果需要,请使用该skip=选项从其他地方开始。

(我的错,忘了指定块大小。)

于 2010-05-24T16:44:48.570 回答
1
cat somefile|head -c `head -c 3 /dev/random |hexdump -d|cut -f4 -d ' '|head -n1`
于 2010-05-24T16:40:40.437 回答
1

如果您的操作系统有 /dev/urandom,那么选择随机数很容易:

RANDNUM=`dd if=/dev/urandom bs=1 count=1 | od -t u1 | cut -f4- -d ' ' | head -1 | sed 's/ //g'`

一旦你有一个随机数,

dd if=input_file of=output_file bs=${RANDNUM}m count=1
于 2010-05-24T17:03:52.480 回答