三国纷争
#cattext.log[t=123]xyzzda,x=abc[t=126]sdjljs,x=abc[t=140]sdsws,x=abc[t=239]dsjdjs,x=wvu[t=248]sdsdess,x=wvu#catx.logabcwvuxxxawk-F'[]=,]''NR==FNR{a[$0];next}a[$6]{b[$6]=b[$6]"\n"$3"t="$2-a[$6]}{a[$6]=$2}END{for(iinb)printf"%s:%s\n\n",i,b[i]}'x.log,text.log#输出:abc:sdjljst=3sdswst=14wvu:sdsdesst=9
蝴蝶不菲
大概像這樣:#datalog="""[t=123]xyzzda,x=abc[t=126]sdjljs,x=abc[t=140]sdsws,x=abc[t=239]dsjdjs,x=wvu[t=248]sdsdess,x=wvu"""#codeimportrefromcollectionsimportdefaultdictdic=defaultdict(list)golden_x,golden_t=None,Noneforlineinlog.split('\n'):line=line.strip()ifnotline:continuem=re.match('\[t=(\d+)\](.+),x=(.+)',line)t,c,x=m.groups()ifx==golden_x:dic[x].append((c,int(t)-golden_t))golden_x,golden_t=x,int(t)forkey,ctindic.items():print(key+':')forc,tinct:print(c,'t='+str(t))print()#resultsabc:sdjljst=3sdswst=14wvu:sdsdesst=9我回答過的問題:Python-QA