用于格式化输出的 Shell 脚本

用于格式化输出的 Shell 脚本
06/26/2017 23:40:40       CAUAJM_I_10082 [aspsun14 connected for IOALPPRXXBD_ALPGLGENFAALL 55443.15215291.1]
06/26/2017 23:40:40       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: STARTING        JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
06/26/2017 23:40:42       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: RUNNING         JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
06/26/2017 23:49:19       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: SUCCESS         JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14        EXITCODE:  0
06/27/2017 23:40:23       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: STARTING        JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
06/27/2017 23:40:24       CAUAJM_I_10082 [aspsun14 connected for IOALPPRXXBD_ALPGLGENFAALL 55443.15236942.1]
06/27/2017 23:40:25       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: RUNNING         JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
06/27/2017 23:48:19       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: SUCCESS         JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14        EXITCODE:  0
06/28/2017 23:41:36       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: STARTING        JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
06/28/2017 23:41:37       CAUAJM_I_10082 [aspsun14 connected for IOALPPRXXBD_ALPGLGENFAALL 55443.15258301.1]
06/28/2017 23:41:38       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: RUNNING         JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14
06/28/2017 23:48:47       CAUAJM_I_40245 EVENT: CHANGE_STATUS    STATUS: SUCCESS         JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14        EXITCODE:  0

我有一个包含上述内容的文件,我想要输出,例如作业名称,然后开始时间,然后结束时间

IOALPPRXXBD_ALPGLGENFAALL 06/26/2017 23:40:40 06/26/2017 23:49:19
IOALPPRXXBD_ALPGLGENFAALL 06/27/2017 23:40:23 06/27/2017 23:48:19
IOALPPRXXBD_ALPGLGENFAALL 06/28/2017 23:41:36 06/28/2017 23:48:47

答案1

awk 对于这些事情来说非常方便。

#!/usr/bin/awk -f

$5 == "CHANGE_STATUS" && $7 == "STARTING" {
    start[$9] = $1 " " $2
}

$5 == "CHANGE_STATUS" && $7 == "SUCCESS" {
    print $9,start[$9],$1,$2
}

$n 是第 n 列。我只是存储开始日期并在找到作业的结束日期后打印所有内容。 (将代码保存在可执行文件中,并通过将输入文件作为参数传递来调用它。) ./script input.txt

如果您绝对需要将其嵌入到 Bash 脚本中,只需执行以下操作:

awk '
    $5 == "CHANGE_STATUS" && $7 == "STARTING" {
        start[$9] = $1 " " $2
    }

    $5 == "CHANGE_STATUS" && $7 == "SUCCESS" {
        print $9,start[$9],$1,$2
    }
' input_file_or_whatever

但要小心在 Awk 脚本本身中对单引号的操作。这可能会变得有点棘手。

相关内容