zoukankan      html  css  js  c++  java
  • python调用linux shell脚本,并返回结果一例

    #!/usr/bin/python
    #-*- coding:cp936 -*-
    import re;
    import os;
    import sys;
    if(__name__=="__main__"):
        p=re.compile('(^\s+|\s+$)');
        pdigit=re.compile('^\d+');
        delimiter='\t';
        strdict='dict.dat';
        strcontigency='contigency.dat';
        str_input=str(sys.argv[1]);
        str_weight=str(sys.argv[2]);
        str_output=str(sys.argv[3]);
        fid_input=file(str_input,'r');
        fid_output=file(str_output,'w');
        keywords=[];
        keywordstmp=fid_input.readlines();
        for m in keywordstmp:
            mykey=p.sub('',m);
            keywords.append(mykey);
        for  keyword in keywords:
            results=[]; 
           #results.append(keyword);
            command='';
            command='grep -n ^%s$  %s'%(keyword,strdict);
            tmpcol=os.popen(command).readlines();
            temp=p.sub('',tmpcol[0]);
            temps=temp.split(':');
            linenum=int(temps[0])+1;  
            command="sed -n '%dp' %s"%(linenum,strdict);
            tmpcol=os.popen(command).readlines();
            temp=p.sub('',tmpcol[0]) ;#indicate howmany docs key indexed;
            totalindexed=temp;
            #results.append(temp);
            command='grep -n ^%s$  %s'%(keyword,str_weight);
            tmpcol=os.popen(command).readlines();
            temp=p.sub('',tmpcol[0]);
            temps=temp.split(':');
            linenum=int(temps[0])+2;  
            command="sed -n '%dp' %s"%(linenum,str_weight);
            tmpcol=os.popen(command).readlines();
            temp=p.sub('',tmpcol[0]) ;
            temps=temp.split(' ');
            myclass=temps[0]
            #results.append(myclass);
            command="grep -n '^%s %s' %s"%(keyword,myclass,strcontigency);
            tmpcol=os.popen(command).readlines();
            temp=p.sub('',tmpcol[0]);
            temps=temp.split(':');
            temp1=p.sub('', temps[1]);
            results=temp1.split(' ');
            results.append(totalindexed);
            #numin=m[1];
            #numnotin=m[2];
            #print numin;
            #print numnotin;
            #print results
            #s=raw_input('please enter');
            #results.append(numin);
            #results.append(numnotin);
            myline=delimiter.join(results);
            fid_output.write(myline);
            fid_output.write('\n');
            print myline;
        fid_input.close();
        fid_output.close();
        print '%s has finished, congratulations!'%str(sys.argv[0]);
  • 相关阅读:
    kettle处理未发现hadoop插件问题
    文档公式编辑神器-Snip
    hive -e执行出现「cannot recognize input near '<EOF>' in select clause」问题
    hive从本地导入数据时出现「Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.MoveTask」错误
    K-Means算法的Python实现
    kettle并行运行时出现「Unknown error in KarafBlueprintWatcher」
    初识 Kafka Producer 生产者
    一次 RocketMQ 进程自动退出排查经验分享(实战篇)
    再谈 RocketMQ broker busy(实战篇)
    RocketMQ 升级到主从切换(DLedger、多副本)实战
  • 原文地址:https://www.cnblogs.com/finallyliuyu/p/2379259.html
Copyright © 2011-2022 走看看