On 9/7/07, Satya <[EMAIL PROTECTED]> wrote:
snip
> If it is possible to do it using the perl or any other scripting
> language, what would be the approach.
snip
It is possible; the approach is the same as in all languages: use
random numbers to select/build strings that match your needs. For
instance, lets say I need to generate a CSV file that has five
columns: id, date, user, severity, and message. Id will be a
sequential number starting at one, the date is just a random date, the
user should be selected from /etc/passwd, the severity is one of low,
med, or high, and message is just a random string between 0 and 50
characters long.
#!/usr/bin/perl
use strict;
use warnings;
use IO::All;
my @passwd = map {(split/:/)[0]} io('/etc/passwd')->getlines;
my @sev = qw<low med high>;
my $recs = shift;
for my $id (1 .. $recs) {
print join(',',
$id,
rand_date(),
$passwd[int rand @passwd],
$sev[int rand @sev],
rand_str()), "\n";
}
#FIXME: this creates invalid dates like 2007-02-30
sub rand_date {
sprintf "%04d-%02d-%02d",
2000 + int rand 7,
1 + int rand 12,
1 + int rand 31;
}
#FIXME: this assumes the ASCII character set
sub rand_str {
join '', map { chr 65 + int rand 26 } 0 .. int rand 51;
}
There is also a module on CPAN called yagg* that can generate data
using a grammar. I haven't used it so I can't really tell you if it
is any good.
* http://search.cpan.org/~dcoppit/yagg/yagg
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/