如何通过多重过滤或搜索提取数据并获得多行使用 bash 脚本

how to extract data with multiple filtering or searching and get multiple line use bash script

我有这样的文件日志格式

输入

2021-02-21 12:12:32 Plugin: 

Table Number: 2001K

Operation: ProcessOpenTablet

Stored Procedure: sp_HDS_OpenTablet

Payload: {"id":"POS026","staff_id":"2","table_no":"2001K","customer_first_name":null}

Response: [{"ErrCode":"01","ErrMsg":""}]

--------------------------------------------------------------------------------------------------------------------------------------------------
2021-02-21 12:12:32 Plugin: 

Table Number: 2001K

Operation: GetListDishFormPOST

Stored Procedure: sp_HDS_GetDishesList

Payload: {"id":"POS026"}

Response: []

--------------------------------------------------------------------------------------------------------------------------------------------------  
2021-02-21 12:12:40 Plugin: 

Table Number: 2001K

Operation: SettlePayment

Stored Procedure: sp_HDS_Payment

Payload: {"id":"POS026","operator_no":"2","table_no":"2001K"}

Response: [{"ErrCode":"01","ErrMsg":""}]

--------------------------------------------------------------------------------------------------------------------------------------------------
2021-02-22 12:18:17 Plugin: 

Table Number: 2002K

Operation: ProcessOpenTablet

Stored Procedure: sp_HDS_OpenTablet

Payload: {"id":"POS027","staff_id":"2","table_no":"2001K","customer_first_name":null}

Response: [{"ErrCode":"01","ErrMsg":""}]

--------------------------------------------------------------------------------------------------------------------------------------------------
2021-02-22 12:18:18 Plugin: 

Table Number: 2002K

Operation: GetListDishFormPOST

Stored Procedure: sp_HDS_GetDishesList

Payload: {"id":"POS027"}

Response: []

--------------------------------------------------------------------------------------------------------------------------------------------------
2021-02-22 12:18:28 Plugin: 

Table Number: 2002K

Operation: SettlePayment

Stored Procedure: sp_HDS_Payment

Payload: {"id":"POS027","operator_no":"2","table_no":"2001K"}

Response: [{"ErrCode":"01","ErrMsg":""}]

--------------------------------------------------------------------------------------------------------------------------------------------------
2021-02-22 15:18:28 Plugin: 

Table Number: 2002K

Operation: SettlePayment

Stored Procedure: sp_HDS_Payment

Payload: {"id":"POS028","operator_no":"2","table_no":"2001K"}

Response: [{"ErrCode":"01","ErrMsg":""}]

--------------------------------------------------------------------------------------------------------------------------------------------------
2021-02-22 12:24:04 Plugin: 

Table Number: 2003K

Operation: ProcessOpenTablet

Stored Procedure: sp_HDS_OpenTablet

Payload: {"id":"POS028","staff_id":"2","table_no":"2001K","customer_first_name":null}

Response: [{"ErrCode":"01","ErrMsg":""}]

--------------------------------------------------------------------------------------------------------------------------------------------------
2021-02-22 12:24:04 Plugin: 

Table Number: 2003K

Operation: GetListDishFormPOST

Stored Procedure: sp_HDS_GetDishesList

Payload: {"id":"POS026"}

Response: []

--------------------------------------------------------------------------------------------------------------------------------------------------

我想提取这个数据看下面的输出,可以动态搜索或过滤,例如过滤日期“2021-02-22”存储过程“sp_HDS_Payment”,结果将是

预期输出

2021-02-22 12:18:28 Plugin: 

Table Number: 2002K

Operation: SettlePayment

Stored Procedure: sp_HDS_Payment

Payload: {"id":"POS027","operator_no":"2","table_no":"2001K"}

Response: [{"ErrCode":"01","ErrMsg":""}]

--------------------------------------------------------------------------------------------------------------------------------------------------
2021-02-22 15:18:28 Plugin: 

Table Number: 2002K

Operation: SettlePayment

Stored Procedure: sp_HDS_Payment

Payload: {"id":"POS028","operator_no":"2","table_no":"2001K"}

Response: [{"ErrCode":"01","ErrMsg":""}]

--------------------------------------------------------------------------------------------------------------------------------------------------

我尝试使用 grep 或 cat 命令来做到这一点。但数据仅限于 1 行,我想要从“日期”(2021-02-22)到“响应”的示例中获取的数据。我该如何解决?

注意事项:filtering/searching 必须与两个条件匹配,在日期输入和存储过程中 = sp_HDS_Payment

尝试以下 awk 解决方案:

 awk -v dat="2021-02-22" -v sp="sp_HDS_Payment" 'BEGIN { RS="--------------------------------------------------------------------------------------------------------------------------------------------------" } [=10=]~dat && [=10=]~sp { ORS=RS;print }' logfile

将记录分隔符设置为破折号,然后仅当记录具有指定的日期 (dat) 和存储过程 (sp) 时才打印记录

记录分隔符确定文件将如何拆分为单独的记录。 $0 ~ .... 然后检查记录 ($0) 是否包含通过 -v 传递给 awk 的 dat 或传递的 sp。

您可以将此 gnu-awk 与自定义 RS 一起使用:

awk -v RS='-{5,}\n' -v dt='^2021-02-22 ' -v sp='sp_HDS_Payment' '{ORS=RT} [=10=] ~ dt && [=10=] ~ "\nStored Procedure: " sp "\n"' log.txt

2021-02-22 12:18:28 Plugin:

Table Number: 2002K

Operation: SettlePayment

Stored Procedure: sp_HDS_Payment

Payload: {"id":"POS027","operator_no":"2","table_no":"2001K"}

Response: [{"ErrCode":"01","ErrMsg":""}]

--------------------------------------------------------------------------------------------------------------------------------------------------
2021-02-22 15:18:28 Plugin:

Table Number: 2002K

Operation: SettlePayment

Stored Procedure: sp_HDS_Payment

Payload: {"id":"POS028","operator_no":"2","table_no":"2001K"}

Response: [{"ErrCode":"01","ErrMsg":""}]

--------------------------------------------------------------------------------------------------------------------------------------------------
2021-02-22 12:18:28 Plugin:

Table Number: 2002K

Operation: SettlePayment

Stored Procedure: sp_HDS_Payment

Payload: {"id":"POS027","operator_no":"2","table_no":"2001K"}

Response: [{"ErrCode":"01","ErrMsg":""}]

--------------------------------------------------------------------------------------------------------------------------------------------------
2021-02-22 15:18:28 Plugin:

Table Number: 2002K

Operation: SettlePayment

Stored Procedure: sp_HDS_Payment

Payload: {"id":"POS028","operator_no":"2","table_no":"2001K"}

Response: [{"ErrCode":"01","ErrMsg":""}]

--------------------------------------------------------------------------------------------------------------------------------------------------

扩展形式:

awk -v RS='-{5,}\n' -v dt='^2021-02-22 ' -v sp='sp_HDS_Payment' '
   {ORS=RT}
   [=11=] ~ dt && [=11=] ~ "\nStored Procedure: " sp "\n"
' log.txt