awk不支持多维数组, 在官方的文档中所说的多维数组并不是传统语言所说的多维数组,awk的多维数组是如:
awk '{ if (max_nf < NF) max_nf = NF max_nr = NR for (x = 1; x <= NF; x++) vector[x, NR] = $x } END { for (x = 1; x <= max_nf; x++) { for (y = max_nr; y >= 1; --y) printf("%s ", vector[x, y]) printf("\n") } }'
在某些情况下需要使用传统多维数组的方式对数据进行统计,生成例如
arr[key] = [field1, filed2, filed3]
这样的数据结构,虽然awk默认不支持这样的数据结构,但是我们可以自己以另一种方式实现,比如上面的数据结构可以变成这样:
arr[key] = "field1@field2@field3"; split(arr[key], arr2, "@")
就是以字符串分割的方式,然后再用split还原, 下面是一个例子,统计下面文件中第一个field重复行的后面字段进行相加,并添加几个filed实现比例统计, 文件是这样的, file.txt:
1270022377 17 0 0 0 0.00 0.00 0.00 1270022399 9 0 0 0 0.00 0.00 0.00 1270022377 17 0 0 0 0.00 0.00 0.00 1270538893 16 8 2 0 50.00 12.50 0.00 1270022399 9 0 0 0 0.00 0.00 0.00 1270538893 16 8 2 0 50.00 12.50 0.00
shell:
awk 'BEGIN{print("Ad View Hit Reg Pay Hit(%) Reg(%) Pay(%)");} \ 1{if($1 in a) { \ split(a[$1], b, "@"); \ a[$1]=sprintf("%s@%s@%s@%s@%s", $1, int($2)+int(b[2]), int($3)+int(b[3]), int($4)+int(b[4]), int($5)+int(b[5])); } \ else { \ a[$1]=sprintf("%s@%s@%s@%s@%s", $1, int($2), int($3), int($4), int($5))} \ }; \ END{for(line in a) { \ split(a[line], c, "@"); \ printf("%s %s %s %s %s %.2f %.2f %.2f\n", c[1], c[2], c[3], c[4], c[5], \ int(c[3])/int(c[2])*100, \ int(c[4])/int(c[2])*100, \ int(c[5])/int(c[2])*100); \ }}' file.txt
结果:
Ad View Hit Reg Pay Hit(%) Reg(%) Pay(%) 1270538893 32 16 4 0 50.00 12.50 0.00 1270022377 34 0 0 0 0.00 0.00 0.00 1270022399 18 0 0 0 0.00 0.00 0.00
2010-04-08 13:36:07, 370 reviews
send to mailbox
g.cn已经被转向到http://www.google.com.hk, 域名是HongKong(香港)的,Google在首页也放出了
欢迎您来到谷歌搜索在中国的新家
字样,目前在google搜索"天安门xx"等敏感词已经没有被屏蔽,
进入google.com也会被跳转到.uk, 不知道为什么,黑暗互联网就要来了,悼念Google
见证历史的时刻到来了,今天,我们一起见证了,全球最伟大的科技公司Google,第一次将其搜索服务撤离一个国家,这不仅仅是中国网民的悲哀,更是中国人的耻辱,这一天具有划时代的历史意义:中国在全球高速发展的互联网科技革命的历史时期,向着闭关锁国的道路迈上了坚定的一步。
2010-03-23 08:38:36, 216 reviews
初试Erlang, 一个小例子
tcp_test.erl
-module(tcp_test). -export([start/0]). start() -> spawn(fun() -> start_listen() end). start_listen() -> {ok, Listen} = gen_tcp:listen(1234, [{packet, 0}, {active, true}]), Pid = spawn(fun() -> par_connect(Listen) end), gen_tcp:controlling_process(Listen, Pid). par_connect(Listen) -> {ok, Socket} = gen_tcp:accept(Listen), Pid = spawn(fun() -> par_connect(Listen) end), loop(Socket). loop(Socket) -> receive {tcp, Socket, Bin} -> case inet:peername(Socket) of {ok, {RemoteIP, _}} -> io:format("Accept connection from ~p~n", [RemoteIP]); {error, Why} -> exit(Why) end, io:format("Accept bin ~p~n", [Bin]), gen_tcp:send(Socket, "you say:" ++ [Bin] ++ "\r\n"), loop(Socket); Any -> io:format("Server ~p~n", [Any]), loop(Socket) end.
2010-03-21 15:42:16, 337 reviews
感觉Mai越来越成熟有女人味了@_@
专辑介绍:日本平成三大歌姬之一仓木麻衣(Mai Kuraki)2010年的第一支单曲《永遠より ながく》在2010年3月3日发售,这是Mai十周年纪念的首支单曲,标题曲目《永遠より ながく》是比较欢快的曲风!!
1. 永遠よりながく2. Drive me crazy3. 永遠よりながく-Instrumental-4. Drive me crazy -Instrumental-
电驴传送门: 踢我
2010-03-05 10:13:32, 366 reviews
NoSQL Database越来越多了,也映射出了Web2.0在数据存储上的特点,数据量大,结构简单。之前有介绍过CouchDB, 这次是Cassandra, Cassandra已经成为Apache的顶级项目(貌似现在Apache下的项目越来越多了..),Cassandra不是简单的key/value存储,支持Hash的数据结构, 如:
Keyspace1.Standard1['jsmith']['first'] = 'John'
\ \ \ \ \
\ \ \_ key \ \_ value
\ \ \_ column
\_ keyspace \_ column family
在存储数据形式方面更有利于把当前的关系数据库进行迁移.
架构上Cassandra支持分布式,容错,写同步等,官方说Rackspace, Digg, Facebook, Twitter, Cisco, Mahalo, Ooyala都在使用Cassandra
个人认为这种NoSQL数据库发展还是非常不错的.
链接
http://blog.evanweaver.com/articles/2009/07/06/up-and-running-with-cassandra/
2010-02-24 10:26:37, 642 reviews
Rails 3 beta 已经发布了, 变化非常大, 细节就不说了, 看这里, 这里, Rails每次的版本变化都是一件头疼的事,gems, plugins版本依赖是个大问题, 升级不是件容易的事情, 况且这次的改动非常大, Rails 3 使用Ruby 1.8.7或者1.9
------------------------
顺带贴下ruby 1.9的变化
2010-02-20 09:15:32, 295 reviews
just DO NOT support IE