生成搜索query

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!/usr/bin/perl -w
use strict;
#==声明变量============================================================
my $fieldList;     				 #字段列表
my $queryFile;                      		 #query文件
my $field;                                       #字段变量
my @arr;
my $arr;
my $index;
my $queryStr = "";
my $queryCount;
my $l = 1;
my $area;
#==逻辑处理===========================================================	
if(@ARGV < 3)
{
	print "Execute error!\n";
	print "Usage: ./queryGen.pl fieldList queryCount\n";
	exit(1);
}
else
{
	$fieldList = $ARGV[1];
	$queryCount = $ARGV[2];
	$area = $ARGV[0];
	$queryFile = "$fieldList.query";
	open(FILEOUT, ">>$queryFile")  || die "can't creat the file:$!\n";
	while($l <= $queryCount)
	{
		open(FILEIN,  "<$fieldList" ) || die "can't open the file:$!\n";
		while($field = <FILEIN>)
		{
			chomp($field);
			@arr = split(/:/, $field);
			if($arr[1] eq 'v')
			{
				$queryStr = $queryStr."&$arr[0]=$arr[2]";
			}
			elsif($arr[1] eq 'a')
			{
				my @valarr = split(/,/, $arr[2]);
				my $val = &getRandItem(@valarr);
				$queryStr = $queryStr."&$arr[0]=$val";
			}
			elsif($arr[1] eq 'f')
			{
				my $fileLineNum = &countFileLine($arr[2]);
				#chomp($fileLineNum);
				#print "The line number is $fileLineNum\n";
				my $fileLine = int(rand($fileLineNum)) + 1;
				chomp($fileLine);
				#print "The rand line is $fileLine\n";
				my $fileItem = `sed -n '$fileLine''p' $arr[2]`;
				chomp($fileItem);
				#print "File item is $fileItem\n";
				$queryStr = $queryStr."&$arr[0]=$fileItem";
			}
		}	
		$queryStr = substr($queryStr, 1);
		print FILEOUT "bin/search?$area?$queryStr";
		$queryStr = "";
		close(FILEIN);
		$l++;
		print FILEOUT "\n";
	}
	close(FILEOUT);
}
#======================================================
sub countFileLine
{
	my $line = 0;
	my($path) = @_;
	open(FD, $path);
	while(<FD>)
	{
		$line++;	
	}
	return $line;
}

#生成不定长度的随机数
sub getRandNum
{
	my $maxLenth;
    my @a;
 	my @c;
 	my $count;
 	my $password;
	#随机串的内容
 	@c=(1..2);
	#生成随机串
 	$count = join '', map { $c[int rand @c] } 0..(0); #0..(0)限制随机串的长度
	#第一个随机串作为下一个随机串的长度
 	$maxLenth=$count; 
	#第二个随机串的内容
 	@a=(1..9);
	#生成最终的随机串
 	$password = join '', map { $a[int rand @a] } 0..($maxLenth-1);
	#返回字串
 	return $password;
}

#从数组中随机取字符或字串
sub getRandStr
{
	#字串的内容
	my @a = ("Y","N");
	#字串的随机位置
	my @c = (0..1);
	my $i = join '', map { $c[int rand @c] } 0..(0);
	#根据随机位置取随机字符
	return $a[$i];
}

#从任意数组中随机取item
sub getRandItem
{
	my @array = @_;
	my $count = scalar(@array) - 1;
	#my $i = join '', map { $c[int rand @c] } 0..(0);
	my $r = int(rand($count));
	return $array[$r];
}