我有一个名为domain的文件,其中包含一些域名.例如:
google.com
facebook.com
...
yahoo.com
Run Code Online (Sandbox Code Playgroud)
我有另一个名为site的文件,其中包含一些网站的URL和数字.例如:
image.google.com 10
map.google.com 8
...
photo.facebook.com 22
game.facebook.com 15
..
Run Code Online (Sandbox Code Playgroud)
现在我要计算每个域名的网址号.例如:google.com有10 + 8.所以我写了一个像这样的awk脚本:
BEGIN{
while(getline dom < "./domain" > 0) {
domain[dom]=0;
}
for(dom in domain) {
while(getline < "./site" > 0) {
if($1 ~/$dom$) #if $1 end with $dom {
domain[dom]+=$2;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但代码if($1 ~/$dom$)并不像我想的那样运行.因为正则表达式中的变量$ dom是按字面解释的.所以,第一个问题是:
有没有办法$dom在正则表达式中使用变量?
然后,因为我刚开始编写脚本
有没有更好的方法来解决我的问题?
我有一个如下数据库表:
create table temperature
(id int unsigned not null auto_increment primary key,
temperature double
);
Run Code Online (Sandbox Code Playgroud)
在我的程序中,我有大约2000万的温度插入表中.我在.Net环境中工作,使用Connector/Net连接到MySql.代码如下:
List<double> temps = new List<double>();
...
string connStr = "server=localhost;user=name;database=test;port=3306;password=*****;";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
conn.Open();
//temps.Count is about 20 million
for (int i = 0; i < temps.Count; i++)
{
string sql1 = "INSERT INTO temperature VALUES (null, "+temps[i]+")";
MySqlCommand cmd1 = new MySqlCommand(sql1, conn);
cmd1.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
conn.Close();
Run Code Online (Sandbox Code Playgroud)
如何尽可能快地插入如此多的行数据?(它只能在我的计算机上每分钟插入2000条记录.)