两个文件的嵌套循环

时间:2018-10-30 06:43:40

标签: linux shell

我有两个测试文件,第一个包含一个第三方名称,第二个文件包含消息状态,例如已发送,失败,技术错误等。

我想在日志文件中搜索每个第三方名称(从第一个文件开始)并获取每个邮件状态的计数(在文件2中列出)

第一文件.txt (第三方名称)

的示例
BNF_IPL
one97
pajwok
RadioAzadi
SPICDIGITAL
U2OPIA
UNIFUN
UNIFUNRS
vectracom
VNTAF
YRMP
INFOTT

第二个file.txt(消息状态):

success
partial
failed
Error absentSubscriber
UnknownSubscriber
smDeliveryFailure
userSpecificReason
CallBarred
systemFailure

我的目标是产生一个包含每个第三方总状态的报告。像

          sent     | failed   | TechErrpr |    Absent | subscriber 
 IBM        someValue  someValue   someValue   someValue   someValue  
Microsoft   someValue  someValue   someValue   someValue   someValue
Oracle      someValue  someValue   someValue   someValue   someValue
google      someValue  someValue   someValue   someValue   someValue

要获取值,我将在日志文件中重复这些名称和状态并获取总计。为此,我试图使用嵌套循环,但没有运气。

for ((i = 0; i <  wc -l  3rdPList.txt ; i++)); do

for ((j = i; j < wc -l status.txt ; j++)); do
     grep 3rdPList.txt logFile | grep status.txt | wc -l > outputFile.txt
    echo $st[j]
  done
done

日志文件示例:

2018-10-30 00:07:19,640 DEBUG [org.mobicents.smsc.library.CdrGenerator] 2018-10-29 14:42:45,789 +0430,588,5,0,93706315646,1,1,temp_failed,BNF_IPL,26674477,0702700006,412012004908984,null,ایید.,Error absentSubscriber after MtForwardSM Request: MAPErrorMessageAbsentSubscriber []
2018-10-30 00:07:41,034 DEBUG [org.mobicents.smsc.library.CdrGenerator] 2018-10-29 16:21:27,260 +0430,588,5,0,0700375593,1,1,temp_failed,BNF_IPL,27008401,null,null,null,عدد1 را به588 ارسال ,AbsentSubscriber response from HLR: MAPErrorMessageAbsentSubscriber []

1 个答案:

答案 0 :(得分:1)

这几乎可以满足您的要求,但是在漂亮的格式方面我并没有做太多的工作!

{ sed 's/^/1,/' 1.txt; sed 's/^/2,/' 2.txt; cat log.txt; } | awk -F, '$1==1{c=substr($0,3);cc[c]++;next} $1==2{s=substr($0,3); ss[s]++;next} {s=$10;c=$11;res[c SEP s]++} END{for(s in ss){printf("%s ",s)};printf("\n");for(c in cc){printf("%s ",c);for(s in ss){printf("%d ",res[c SEP s]+0)}printf("\n")}}' 

示例输出

systemFailure temp_failed CallBarred userSpecificReason smDeliveryFailure UnknownSubscriber Error absentSubscriber partial success 
pajwok 0 0 0 0 0 0 0 0 0 
SPICDIGITAL 0 0 0 0 0 0 0 0 0 
YRMP 0 0 0 0 0 0 0 0 0 
UNIFUN 0 0 3 0 0 0 0 0 0 
U2OPIA 0 0 0 0 0 0 0 0 0 
UNIFUNRS 0 0 0 0 0 0 0 0 0 
RadioAzadi 0 0 0 0 0 0 0 0 0 
one97 0 0 0 0 0 0 0 0 0 
BNF_IPL 0 2 0 0 0 0 0 0 0 
VNTAF 0 0 0 0 0 0 0 0 0 
INFOTT 0 0 0 0 0 0 0 0 0 
vectracom 0 0 0 0 0 0 0 0 0 

如果您想了解它,请尝试分别运行各部分。因此,在第一部分中,我为所有公司名称加了1前缀,以便awk可以将它们与状态代码和日志行区分开来:

sed 's/^/1,/' 1.txt

输出

1,BNF_IPL
1,one97
1,pajwok
1,RadioAzadi
1,SPICDIGITAL
1,U2OPIA
1,UNIFUN
1,UNIFUNRS
1,vectracom
1,VNTAF
1,YRMP
1,INFOTT

然后,我为所有状态消息加上一个2前缀,以便awk可以将它们与公司名称和日志行区分开:

sed 's/^/2,/' 2.txt

输出

2,success
2,partial
2,temp_failed
2,Error absentSubscriber
2,UnknownSubscriber
2,smDeliveryFailure
2,userSpecificReason
2,CallBarred
2,systemFailure

然后我将日志文件cat放入awk

cat log.txt

awk可以写成多行并带有注释:

{ sed ...; sed ...; cat ...; } | awk -F, '
    $1==1 {c=substr($0,3); cc[c]++; next}  # Process company name in "1.txt", "c" holds name, "cc[]" is an array of names
    $1==2 {s=substr($0,3); ss[s]++; next}  # Process status code in "2.txt, "s" holds status, "ss[]" is an array of statuses
          {s=$10; c=$11; res[c SEP s]++}   # Process line from log, status is field 10, company is field 11. Increment results array "res[]"
    END   {       
            # Print line of status codes
            for(s in ss){printf("%s ",s)};
            printf("\n");
            for(c in cc){printf("%s ",c);
               for(s in ss){printf("%d ",res[c SEP s]+0)}printf("\n")}
    }' 

SEP只是伪2D数组的分隔符。