Search code examples
shellawkgrepcut

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


I have file log format like this

Input

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: []

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

I want to extract this data look output bellow and can dynamically search or filter, example filter the date "2021-02-22" Stored Procedure "sp_HDS_Payment", and the result will be

Expected Output

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":""}]

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

I was tryin to use the grep or cat commands do that. but the data is only limited to 1 line, I want the data obtained example from "date" (2021-02-22) to "Response". How do I resolve this?

NOTES : filtering/searching must be match with two condition, in DATE input AND Stored Procedure = sp_HDS_Payment


Solution

  • Try the following awk solution:

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

    Set the record separator to dashes and then print the record only if it has the spcified date (dat) and stored procedure (sp)

    The record separator determines how the file is going to be split into individual records. $0 ~ .... then checks to see if the record ($0) contains the dat that is passed into awk with -v or the sp that is passed.