美文网首页
统计个数

统计个数

作者: rong酱 | 来源:发表于2021-01-05 19:35 被阅读0次
    for i in `cat file3 | awk -F " " '{print $1}' | sort | uniq`
    do
    
        fc_count=`grep ${i} file3 | wc -l `
        for y in `cat file3 | awk -F " " '{print $2}' | sort | uniq`
        do
            sample_count=`grep ${y} file3 | wc -l `
            sample_fc_count=`grep ${y} file3 | grep ${i} | wc -l `
            echo ${i} ${y} ${sample_fc_count} >>count.txt
            echo ${y} ${sample_count} >>count.txt
        done
        echo ${i} ${fc_count} >>count.txt
    done
    

    另一种写法

    cat file3 | awk -F " " '{print $2}' | sort -u | uniq -c
    
    cat file3 | sort -u | uniq -c
    

    python脚本

    #!/usr/bin/env python
    #-*-coding:utf-8-*-
    
    import os
    import re
    
    file=open("file3","r")
    
    # FC 和 sample 各存一个字典
    dictfc = {}
    dictsample = {}
    dictfcsam= {}
    # 按照行 读文件;
    fileline=file.readlines()
    
    for line in fileline:
        key = line.strip().split("\t")
    #    dict[key[0]]=key[1]
        fc = key[0]
        sample = key[1]
    # fc存入字典
        if fc not in dictfc:
            dictfc[fc] = 1
        else:
            dictfc[fc] += 1
    # sample存入字典        
        if sample not in dictsample:
            dictsample[sample] = 1
        else:
            dictsample[sample] += 1
        if line not in dictfcsam:
            dictfcsam[line] = 1
        else:
            dictfcsam[line] += 1
    
    print(dictfc)  
    print(dictsample)  
    print(dictfcsam)    
     
    file.close()
    

    相关文章

      网友评论

          本文标题:统计个数

          本文链接:https://www.haomeiwen.com/subject/ieiwoktx.html