guopengfeiheze 2020-07-28 09:16 采纳率: 50%
浏览 58
已采纳

awk 执行语句语法问题

犹如下的文本内容:

2020-07-22 17:27:21.933721 [INFO] mrcp_client.c:373 () Register RTP Termination Factory [voxeo-prophecy8.0-mrcp1]
2020-07-22 17:27:21.933724 [INFO] mrcp_client.c:390 () Register RTP Settings [RTP-Settings]
2020-07-22 17:27:21.933728 [INFO] mrcp_client.c:431 () Register Signaling Settings [Signaling-Settings]
2020-07-22 17:27:21.933732 [INFO] mrcp_client.c:407 () Register Signaling Agent [voxeo-prophecy8.0-mrcp1]
2020-07-22 17:27:21.933736 [INFO] mrcp_client.c:598 () Register Profile [voxeo-prophecy8.0-mrcp1]
2020-07-22 17:27:21.933747 [INFO] mrcp_client.c:648 () Register Application [synth]
2020-07-22 17:27:21.933766 [INFO] mrcp_client.c:648 () Register Application [recog]
2020-07-22 17:36:22.450615 [INFO] mrcp_client.c:700 (TTS-0) Add MRCP Handle TTS-0 <new>
2020-07-22 17:36:22.470612 [INFO] mrcp_client_connection.c:435 (TTS-0) Add Control Channel <d1518564cbfe11ea@speechsynth> 172.16.5.35:41610 <-> 172.16.5.90:1544 [1]
2020-07-22 17:36:22.470612 [INFO] mrcp_client_connection.c:530 (TTS-0) Send MRCPv2 Data 172.16.5.35:41610 <-> 172.16.5.90:1544 [166 bytes]
2020-07-22 17:36:22.470612 [INFO] mrcp_client_connection.c:635 () Receive MRCPv2 Data 172.16.5.35:41610 <-> 172.16.5.90:1544 [83 bytes]
2020-07-22 17:36:24.770610 [INFO] mrcp_client_connection.c:635 () Receive MRCPv2 Data 172.16.5.35:41610 <-> 172.16.5.90:1544 [122 bytes]
ab1ae060-cca9-11ea-ad48-05ac15c48ae8 2020-07-23 13:59:28.801997 [DEBUG] switch_ivr_play_say.c:3025 OPEN TTS unimrcp
ab1ae060-cca9-11ea-ad48-05ac15c48ae8 2020-07-23 13:59:28.801997 [DEBUG] switch_ivr_play_say.c:3035 Raw Codec Activated
ab1ae060-cca9-11ea-ad48-05ac15c48ae8 2020-07-23 13:59:28.801997 [DEBUG] switch_ivr_play_say.c:2729 Speaking text: (null)

我的业务是,根据关键字和日志级别以及日志时间分页过滤并返回查询到的日志,我使用下面两个查询,结果差异很大(不加关键字根本查询不出来)
第一个(有关键字、日志日期、日志级别INFO,能查询出来):

awk 'BEGIN{i=1;j=i+99}
{
    isFirst = -1;
    longDate= ""; 
    paramDate="";
    if(match($1, /[0-9]{4}-[0-9]{2}-[0-9]{2}/)>0)
    {
        isFirst = 1;
        split($2, arr, ".");
        ymd=$1;
        hms=arr[1];
        gsub("-", " ", ymd);
        gsub(":", " ",hms);
        newDate = (ymd" "hms);
        longDate =  mktime(newDate);
        paramDate=mktime(/2020 07 22 13 03 34/);
    } else if(match($2, /[0-9]{4}-[0-9]{2}-[0-9]{2}/)>0)
    {
        isFirst = 0;
        split($3, arr, ".");
        ymd=$2;
        hms=arr[1];
        gsub("-", " ", ymd);
        gsub(":", " ",hms);
        newDate = (ymd" "hms);
        longDate =  mktime(newDate);
        paramDate=mktime(1);
    }
    if(longDate >= paramDate && $0  ~ /mrcp_client.c/ && i<=j && isFirst != -1)
    {
        if(isFirst == 1)
        {
            if($3 ~ /INFO/)
                print $0 
            }else
            {
                if($4 ~ /INFO/)
                {
                    $1=""; 
                    print $0
                }
            }
            i=i+1;
        }
    }' /usr/local/freeswitch/log/freeswitch.log

第二个(无查询的关键字,仅仅根据日志级别INFO、日志时间去查询,直接查询不到):

awk 'BEGIN{i=1;j=i+99}
{
    isFirst = -1;
    longDate= ""; 
    paramDate="";
    if(match($1, /[0-9]{4}-[0-9]{2}-[0-9]{2}/)>0)
    {
        isFirst = 1;
        split($2, arr, ".");
        ymd=$1;hms=arr[1];
        gsub("-", " ", ymd);
        gsub(":", " ",hms);
        newDate = (ymd" "hms);
        longDate =  mktime(newDate);
        paramDate=mktime(/2020 07 22 13 03 34/);
    } else if(match($2, /[0-9]{4}-[0-9]{2}-[0-9]{2}/)>0)
    {
        isFirst = 0;
        split($3, arr, ".");
        ymd=$2;hms=arr[1];
        gsub("-", " ", ymd);
        gsub(":", " ",hms);newDate = (ymd" "hms);
        longDate =  mktime(newDate);
        paramDate=mktime(1);
    }if(longDate >= paramDate && i<=j && isFirst != -1)
    {
        if(isFirst == 1)
        {
            if($3 ~ /INFO/)
            {
                print $0 
            }else
            {
                if($4 ~ /INFO/)
                {
                    $1=""; 
                    print $0
                }
            }
            i=i+1;
        }
    }' /usr/local/freeswitch/log/freeswitch.log

这两个查询只有一个地方有差异,那就是在

if(longDate >= paramDate && $0 ~ /mrcp_client.c/ && i<=j && isFirst != -1)

一直搞不明白,为什么一个能查出来,一个就查不出来呢,是我的语法不标准吗?还请大佬们指点下。

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 已采纳回答 10月27日

      悬赏问题

      • ¥100 CVI2012 做暗通道图像去雾思路详细步骤
      • ¥15 用Java实现双端队列
      • ¥150 ID3决策树实现分类
      • ¥15 multisim10安装后,找不到NI License Manager的程序来安装许可证
      • ¥15 C++银行卡系统 Help!
      • ¥15 R语言数据分析的相关问题
      • ¥15 模型导入SP后贴图纹理只有一个,拆了四张UV的,怎么解决?
      • ¥15 检索带order by 非常慢
      • ¥20 python 爬虫 token 加密方式
      • ¥20 IAR环境报错reading CPU status failed