如何避免在日志文件中打印特殊记录

时间:2019-05-02 13:40:12

标签: linux shell scripting

我正在尝试从日志文件中打印一些日志,以避免打印特定的日志。

我有一个如下所示的日志文件:

SOH
     +++ skdfhahjsahsdjk >
    ** ALPHA EDFJDJFKLJDKFJKSDLFJL
       SDFSDGDRGRTG
       WEFETTFYRT #168113++-
   SOH 
       +++ skdfhahjsahsdjk >
    * KJENRFKES DFJKLSDFJEDFJDJFKLJDKFJKSDLFJL
       SDFSDGDRGRTG
       WEFETTFYRT #168113++-
   SOH 
       +++ skdfhahjsahsdjk >
    CL mesukww juwaehdiearfa
       SDFSDGDRGRTG
       WEFETTFYRT #168113++-
    SOH
       +++ skdfhahjsahsdjk >
    *C KJENRFKESDFJ ksludhieokdaewmdp
       SDFSDGDRGRTG
       WEFETTFYRT #168113++-
    SOH
     +++ skdfhahjsahsdjk >
    ** BETA EDFJDJFKLJDKFJKSDLFJL
       SDFSDGDRGRTG
       WEFETTFYRT #168113++-
   SOH
    ** Gama EDFJDJFKLJDKFJKSDLFJL
       SDFSDGDRGRTG
       WEFETTFYRT #168113++-  

我可以使用以下脚本在特殊字符SOH和以**开头的日志之间打印日志:

awk '/^[    ]*SOH[  ]*$/ {                # match SOH line
   if(found && length(buf)>0) print buf;  # print old buffer
   buf="";                                # clear buffer
   found=0;                               # no ** line yet
   skip=1;                                # do not add SOH line to buffer
}
/^[     ]*\*\*[     ]/ { found=1;}        # ** line found. Edit: The second [   ] (with space and tab) makes sure there are exactly two *
{                                         # for any line...
   if(!skip) buf=buf  $0  "\n";           # add line to buffer (except SOH)
   skip=0;                                # do not skip next line
}
END {                                     # to handle matching last record
   if(found && length(buf)>0) print buf;  # print old buffer
}' logfile

我正在尝试避免打印特定记录,例如与“ ALPHA”相关的日志。

我尝试通过下面的代码,但未成功:

cat logfile |grep -v "ALPHA"> result_log

结果:

SOH 
 +++ skdfhahjsahsdjk >

   SDFSDGDRGRTG
   WEFETTFYRT #168113++-

SOH
 +++ skdfhahjsahsdjk >
** BETA EDFJDJFKLJDKFJKSDLFJL
   SDFSDGDRGRTG
   WEFETTFYRT #168113++-
SOH
** Gama EDFJDJFKLJDKFJKSDLFJL
   SDFSDGDRGRTG
   WEFETTFYRT #168113++-

从上面可以看到,尽管“ ALPHA本身”,它似乎仍打印出包含“ ALPHA” m的其余日志。

预期结果应为:

       SOH
         +++ skdfhahjsahsdjk >
        ** BETA EDFJDJFKLJDKFJKSDLFJL
           SDFSDGDRGRTG
           WEFETTFYRT #168113++-
       SOH
        ** Gama EDFJDJFKLJDKFJKSDLFJL
           SDFSDGDRGRTG
           WEFETTFYRT #168113++-

因此,它根本不应该打印包含“ ALPHA”的日志。

如果您能提出任何解决方案,我将不胜感激!

谢谢!

0 个答案:

没有答案