English 中文(简体)
随意造就许多条形状,不填满。
原标题:get many lines randomly form a file without shuffling them

我需要使用UNIX指挥线的文字,从一起整理的档案中抽取11条随机线。 例

Random source file:

1
2
3
4
5
6
7
8
9
10
11
############
12
13
14
15
16
17
18
19
20
21
22

页: 1 请让我:

1
2
3
4
5
6
7
8
9
10
11

12
13
14
15
16
17
18
19
20
21
22
最佳回答
tail -n +$(($RANDOM % 
     ($(wc -l "$filename" | cut -d    -f1) - 11))) "$filename" |
     head -n 11

Tested

页: 1

export filename=/etc/dictionaries-common/words
set -o xtrace

第一次

~$ tail -n +$(($RANDOM % ($(wc -l "$filename" | cut -d    -f1) - 11))) "$filename" | head -n 11
+ head -n 11
++ wc -l /etc/dictionaries-common/words
++ cut  -d   -f1
+ tail -n +11614 /etc/dictionaries-common/words
Moriarty s
Morin
Morin s
Morison
Morison s
Morita
Morita s
Morley
Morley s
Mormon
Mormon s

第二次

~$ tail -n +$(($RANDOM % ($(wc -l "$filename" | cut -d    -f1) - 11))) "$filename" | head -n 11
+ head -n 11
++ wc -l /etc/dictionaries-common/words
++ cut  -d   -f1
+ tail -n +1661 /etc/dictionaries-common/words
Beatrice
Beatrice s
Beatrix
Beatrix s
Beatriz
Beatriz s
Beau
Beau s
Beaufort
Beaufort s
Beaujolais
问题回答

你可以尝试:

n=$((`wc -l "${file}" | cut -d    -f1` / 11))
m=$(($RANDOM % $n))
tail -n +$(($m * 11)) "${file}" | head -n 11




相关问题
Weighted random numbers

I m trying to implement a weighted random numbers. I m currently just banging my head against the wall and cannot figure this out. In my project (Hold em hand-ranges, subjective all-in equity ...

Comprehensive information about hash salts

There are a lot of questions about salts and best practices, however most of them simply answer very specific questions about them. I have several questions which feed into one another. Assuming a ...

Generate unique names?

I am working on a php site in which we have to upload images from users.i have to rename that file for preventing conflicts in the name of the image. uniqid(rand(), true); and adding a large random ...

How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

热门标签