如何使脚本生成文件,从百分比分配插入文本? [英] How to make a script to generate files and insert text from a percentage distribution?
问题描述
我在bash / perl的脚本真正的新手,但必须拿出一个解决这个问题,我有。
I'm a true novice with bash/perl scripting but have to come up with a solution to this problem I have.
我需要生成从 1-9
, A到Z
和<$英文字母命名的文件C $ C> A到Z 。
所以总共会有61的文件。
I need to generate files named after the english alphabet from 1 to 9
, a to z
and A to Z
.
So in total there will be 61 files.
之后,我需要根据pre-定义分布插入文本到每一个文件,说:
After that I need to insert text to each file according to a pre-defined distribution, say:
text1 80%
text2 10%
text3 5%
text4 2%
text5 2%
text6 1%
当每个文件只包含一个文本,并从这个例子是有80%的机会,该文件名为 C
例如包含文本文本1
。同样,名为 7
将有5%的几率包含文本文字3
。
When each file contains only one text and from the example there is a 80% chance that the file named C
for instance will contain the text text1
. Similarly, The file named 7
will have 5% chance to contain the text text3
.
我的问题是从哪里开始?什么是preferred的bash或Perl?我想生成的文件会有些容易,但呼应的文字根据分配的文件将是棘手的。
My question is where to start? what is preferred bash or perl? I think generating the files would be somewhat easy but echoing text to files according to a distribution will be tricky.
这是我在哪里使用bash:
This is where I'm at using bash:
创建一个文件,然后输入相同的文字:
Create files and enter same text:
for f in {a..z} {A..Z} {1..9}
do
echo text > "$f"
done
编辑:这是ubuntu下10.4
It's under ubuntu 10.4
推荐答案
创建文件是在bash小事,填充它们不是那么干净,但不是所有的坏:
Creating the files is trivial in bash, filling them isn't quite so clean but isn't all that bad:
for file in {1..9} {a..z} {A..Z}; do
(
line=$((RANDOM % 100))
if (( line < 80 )); then
echo text1
elif (( line < 90 )); then
echo text2
elif (( line < 95 )); then
echo text3
[...]
fi
) >$file
done
$ RANDOM
不是随机性的重要来源,但它可能是罚款这一点。
$RANDOM
isn't a great source of randomness, but it's probably fine for this.
这篇关于如何使脚本生成文件,从百分比分配插入文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!