题目在这里 ,bbs有问题,我的几个回帖都被删了.
-
#!/usr/bin/env python
-
-
import subprocess
-
-
spec ={'node': ((53,102),(103,122),(223,376)),
-
-
'renderG' : (('1-166','181-202'),('167-180','203-204'))
-
}
-
-
cmd ="qbhosts | awk '/node/||/renderG/'"
-
-
output = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE).communicate()[0]
-
-
d ={}
-
-
for i in output.splitlines():
-
-
items = i.split()
-
if not items[0].endswith("xxx.com"):
-
items[0]=items[0]+".xxx.com"
-
d[items[0]] = items[3]
-
-
for k,v in spec.items() :
-
-
for i in v:
-
# set default value for m1,m2.
-
m1=m2=None
-
-
if type(i[0]) == str or type(i[1]) == str :
-
-
begin = i[0].split("-")[0]
-
m1 = i[0].split("-")[1]
-
m2 = i[1].split("-")[0]
-
end = i[1].split("-")[1]
-
host = [ "%s-%03d.xxx.com" %(k,m ) for m in xrange(int(begin),int(end)+1) if m not in xrange(int(m1)+1,int(m2))]
-
-
else :
-
begin,end = i[0],i[1]
-
host = [ "%s-%03d.xxx.com" %(k,m) for m in xrange(begin,end+1) ]
-
#print "--------\n",host,"\n----------------"
-
-
temp = {}
-
-
for h in host :
-
-
if h in d.keys():
-
temp[h] = d[h]
-
-
#else : print "%s doesn't exist."%h
-
-
act = len([k1 for k1,v1 in temp.items() if v1 =="active" ])
-
down = len([k1 for k1,v1 in temp.items() if v1 =="down" ])
-
-
if m1 and m2 :
-
-
print "\n%s[%03d-%03d,%03d-%03d].xxx.com have %d active node(s), %d down node(s)\n" % (k,int(begin),int(m1),int(m2),int(end),act,down)
-
-
else:
-
print "\n%s[%03d-%03d].xxx.com have %d active node(s), %d down node(s)\n" % (k,int(begin),int(end),act,down)
-
-
temp.clear()
相对而言,awk的简洁一些,但有些问题,
-
awk 'NR==FNR{
-
-
for(i=$2;i<=$3;i++){a[$1" "i]=$2;b[$1" "$2]=$3;}
-
-
if(NF>3) { b[$1" "$2]=$3" Exclude ["$4"-"$5"]";
-
-
-
for(i=$4;i<=$5;i++)
-
-
-
delete a[$1" "i]
-
-
}
-
-
-
}NR>FNR{
-
-
-
-
split($1,c,"[-.]")
-
-
-
n=c[1]" "(c[2]+0)
-
-
-
if(n in a) d[c[1]" "a[n]][$4]++ # d[node" "53]["active"] ++ ; d[node" "103]["down"] ++
-
-
-
}END{for(i in d)for(j in d[i])print i"-"b[i],j,d[i][j]}'
gawk 4.0+ 可用。
题外话:要处理文件,截取固定的行,read/write都有3个,read/readline/readlines , read每次只读取若干字节,readline、readlines很相似,后者是一次性读取完,前者逐行(必须有while True之类的来保证继续),而且数据类型也不同,readlines是一个list.
他们都区别还在于是否有尾部的\n,要想往回读文件,要seek了。
例如:
with open("sample","r") as f:
for i in f.readlines()[2:-4]:
s =i.split()
print "INSERT INTO tab VALUES (%s )"%",".join("'%s'"%i for i in s)
f.close()
阅读(1561) | 评论(0) | 转发(0) |