根据会话ID合并文件中的2种类型的行

时间:2013-12-03 04:27:07

标签: awk

文件1

name=USER_TRANSACTION,type=0,session=111,x0,y0,z0  
name=QUOTA,session=111,10,20,30  

name=USER_TRANSACTION,type=1,session=111,x1,y1,z1  
name=QUOTA,session=111,11,21,31  

name=USER_TRANSACTION,type=0,session=112,x0,y0,z0  
name=USER_TRANSACTION,type=1,session=112,x1,y1,z1  
name=QUOTA,session=112,11,21,31  

name=USER_TRANSACTION,type=0,session=113,x0,y0,z0  
name=QUOTA,session=99999,1,2,3  
name=QUOTA,session=113,11,21,31  

name=USER_TRANSACTION,type=0,session=114,x0,y0,z0  
name=USER_TRANSACTION,type=1,session=114,x1,y1,z1  
name=QUOTA,session=114,1x,2x,3x  
name=QUOTA,session=114,104,204,304  

name=USER_TRANSACTION,type=0,session=115,x0,y0,z0  
name=QUOTA,session=116,11,21,31  
name=USER_TRANSACTION,type=0,session=117,x0,y0,z0  
name=QUOTA,session=117,117,217,317  
name=QUOTA,session=115,105,205,305  

输出

name=USER_TRANSACTION,type=0,session=111,x0,y0,z0,name=QUOTA,10,20,30
name=USER_TRANSACTION,type=1,session=111,x1,y1,z1,name=QUOTA,111,11,21,31
name=USER_TRANSACTION,type=0,session=112,x0,y0,z0,NOQUOTA
name=USER_TRANSACTION,type=1,session=112,x1,y1,z1,name=QUOTA,11,21,31
name=USER_TRANSACTION,type=0,session=113,x0,y0,z0,name=QUOTA,11,21,31
name=QUOTA,session=99999,1,2,3,NOUSER
name=USER_TRANSACTION,type=0,session=114,x0,y0,z0,NOQUOTA
name=USER_TRANSACTION,type=1,session=114,x1,y1,z1,name=QUOTA,104,204,304
name=QUOTA,session=114,1x,2x,3x,NOUSER
name=USER_TRANSACTION,type=0,session=115,x0,y0,z0,name=QUOTA,105,205,305
name=QUOTA,session=116,11,21,31,NOUSER
name=USER_TRANSACTION,type=0,session=117,x0,y0,z0,name=QUOTA,117,217,317

我尝试了一些东西,但没有多少帮助如何开始这个...因为它是相同的文件,我们必须用另一行分析每一行

我尝试过以下代码,但未提供所需的输出

{
        if($0~/USER_TRANSACTION/)
        {
                if($3 in ccr)
                        print ccr[$3]",NOQUOTA";
                ccr[$3]=$0;
        }
        if($0~/QUOTA/)
        {
                if($3 in ccr)
                {
                        print ccr[$3]","$0;
                        delete ccr[$3];
                }
                else
                {
                        print $0",NOUSER"
                }
        }
}
END{for (i in ccr)print ccr[i]",NOMATCH";}

输出不正确

  

名= USER_TRANSACTION,类型= 0,会话= 111,X0,Y0,Z0,名字=定额,会话= 111,10,20,30     名称= USER_TRANSACTION,类型= 1,会话= 111,X1,Y1,Z1,名字=定额,会话= 111,11,21,31     名称= USER_TRANSACTION,类型= 0,会话= 112,X0,Y0,Z0,NOQUOTA     名称= USER_TRANSACTION,类型= 1,会话= 112,X1,Y1,Z1,名字=定额,会话= 112,11,21,31     名称= USER_TRANSACTION,类型= 0,会话= 113,X0,Y0,Z0,名字=定额,会话= 99999,1,2,3     名称=名额,会话= 113,11,21,31,NOUSER     名称= USER_TRANSACTION,类型= 0,会话= 114,X0,Y0,Z0,NOQUOTA     名称= USER_TRANSACTION,类型= 1,会话= 114,X1,Y1,Z1,名字=定额,会话= 114,1x,2X,3X     名称=名额,会话= 114104204304,NOUSER     名称= USER_TRANSACTION,类型= 0,会话= 115,X0,Y0,Z0,名字=定额,会话= 116,11,21,31     名称= USER_TRANSACTION,类型= 0,会话= 117,X0,Y0,Z0,名字=定额,会话= 117117217317     名称=定额,会话= 115105205​​305,NOUSER

1 个答案:

答案 0 :(得分:0)

您可以尝试:

awk -f merge.awk file1 file1 

其中merge.awk

# Rules: The terms "first" and "last" refer to line positions in the
# file. Then: 
# 1) the quota for a given user transaction is either:
#   a) if there is no quota defined with given session id -> "NOQUOTA"
#   b) if there is only defined a single quota for the given session
#     id: b1) it is assigned to the last user transaction with the
#             given id
#         b2) all other user transactions with the same id is assigned
#             with "NOQUOTA"
#   c) if there is defined multiple quota with the given session id:
#       c1) if there are no other user transactions with the same
#           session id between the given quotas, the user transaction
#           is assigned the last quota
#       c2) else each user transaction (with the same id) is assigned
#          the last quota given before another user transaction with
#          the same id occurs
# 2) if there is no user transaction for a given quota (with the same
#      id) the quota is output with the label "NOUSER"
# 


NR==FNR {
    if ($0 ~ /name=/) {
        sub(/ +$/,"") 
        getSessionInfo()
        if ($0 ~ /name=USER/) {
            m[id]=type+1
        } else {
            if (m[id]) {
                tt=m[id]-1
                if ((id,tt) in qu) {
                    dq[qu[id,tt]]++
                }
                qu[id,tt]=$0
            }   
        }
    }
    next
}

/name=/ {
    sub(/ +$/,"") 
    getSessionInfo()
    if (match($0,/name=USER/)) fu=1
    else fu=0
    if (fu) {
        if ( !((id,type) in qu))
            print $0 ",NOQUOTA"
        else {
            qd=qu[id,type]
            sub(/session=[^,]*,/,"",qd)
            print $0 "," qd
            dd[id]++
        }
    }
    else { # quota
        if ($0 in dq)
            print $0 ",NOUSER"
        else {
            if (dd[id]) 
                dd[id]--
            else
                print $0 ",NOUSER"
        }
    }
}

function getSessionInfo(a) {
    match($0,/session=([^,]*),/,a)
    id=a[1]
    if (match($0,/type=([^,]*),/,a))
        type=a[1]
    else
        type=""
}

带输出:

name=USER_TRANSACTION,type=0,session=111,x0,y0,z0,name=QUOTA,10,20,30
name=USER_TRANSACTION,type=1,session=111,x1,y1,z1,name=QUOTA,11,21,31
name=USER_TRANSACTION,type=0,session=112,x0,y0,z0,NOQUOTA
name=USER_TRANSACTION,type=1,session=112,x1,y1,z1,name=QUOTA,11,21,31
name=USER_TRANSACTION,type=0,session=113,x0,y0,z0,name=QUOTA,11,21,31
name=QUOTA,session=99999,1,2,3,NOUSER
name=USER_TRANSACTION,type=0,session=114,x0,y0,z0,NOQUOTA
name=USER_TRANSACTION,type=1,session=114,x1,y1,z1,name=QUOTA,104,204,304
name=QUOTA,session=114,1x,2x,3x,NOUSER
name=USER_TRANSACTION,type=0,session=115,x0,y0,z0,name=QUOTA,105,205,305
name=QUOTA,session=116,11,21,31,NOUSER
name=USER_TRANSACTION,type=0,session=117,x0,y0,z0,name=QUOTA,117,217,317