Ubuntu: tail -f grep for one paragraph



Question:

I have a flowing log file that is separated by empty lines. It's difficult to tail it with grep 'PID' - when I do that it shows only 1 line.

This is the Sample:

[7/12/16 17:00:12:206 WIB] 0000013d ht.integration.js.JavaScriptIntegrationLibraryImplementation I {  } [project worklight_server]    [7/12/16 17:00:12:382 WIB] 00000144 ht.integration.js.JavaScriptIntegrationLibraryImplementation I {  "array": [    {       "activation_aging": 60,       "api_temp_path": "\/opt\/lampp\/htdocs\/advanced\/api\/web\/temp\/",       "backend_temp_path": "\/opt\/lampp\/htdocs\/advanced\/backend\/web\/temp\/",       "cc_number": "1500599",       "common_id": 1,       "credit_interest_rate": "11.20",       "enable_otr_new": 1,       "enable_otr_owned": 1,       "env_info": "DEV",       "insurance_interest_rate": 20,       "is_contract_mandatory": 1,       "min_dp_percentage": 20,       "otr_threshold": 10000000,       "radius_length": 10,       "spa_words": "cumigalak",       "token_aging": 10    }  ],  "isSuccessful": true,  "responseHeaders": {    "Connection": "Keep-Alive",    "Content-Length": "625",    "Content-Type": "text\/html; charset=UTF-8",    "Date": "Tue, 12 Jul 2016 09:59:38 GMT",    "Keep-Alive": "timeout=5, max=99",    "Server": "Apache\/2.4.17 (Unix) OpenSSL\/1.0.1p mod_perl\/2.0.8-dev Perl\/v5.16.3",    "X-Powered-By": "Nintriva <nintriva.com>"  },  "responseTime": 46,  "statusCode": 200,  "statusReason": "OK",  "totalTime": 47  } [project worklight_server]  [7/12/16 17:00:12:406 WIB] 0000013d ht.integration.js.JavaScriptIntegrationLibraryImplementation I {  "imei": "5957389338873327"  } [project worklight_server]  [7/12/16 17:00:12:610 WIB] 0000013d com.worklight.integration.model.ProcedureInvoker             W FWLSE0319W: Backend response content type 'json' did not match the expected content type 'text/html', continue prossesing the response. The request and response headers and body: request:  /advanced/api/web/common/generate/  imei=5957389338873327    response:  OK  Date=Tue, 12 Jul 2016 09:59:39 GMT  Server=Apache/2.4.17 (Unix) OpenSSL/1.0.1p mod_perl/2.0.8-dev Perl/v5.16.3  X-Powered-By=Nintriva <nintriva.com>  Content-Length=66  Keep-Alive=timeout=5, max=98  Connection=Keep-Alive  Content-Type=text/html; charset=UTF-8  [project worklight_server]  [7/12/16 17:00:12:611 WIB] 0000013d ht.integration.js.JavaScriptIntegrationLibraryImplementation I {  "data": {    "token_id": "xxx"  },  "isSuccessful": true,  "responseHeaders": {    "Connection": "Keep-Alive",    "Content-Length": "66",    "Content-Type": "text\/html; charset=UTF-8",    "Date": "Tue, 12 Jul 2016 09:59:39 GMT",    "Keep-Alive": "timeout=5, max=98",    "Server": "Apache\/2.4.17 (Unix) OpenSSL\/1.0.1p mod_perl\/2.0.8-dev Perl\/v5.16.3",    "X-Powered-By": "Nintriva <nintriva.com>"  },  "responseTime": 203,  "status": 1,  "statusCode": 200,  "statusReason": "OK",  "totalTime": 203  } [project worklight_server]  [7/12/16 17:00:12:800 WIB] 00000144   something line 1  something line 2  [project worklight_server]  

I just want to see this paragraph containing PID = 00000144

[7/12/16 17:00:12:382 WIB] 00000144 ht.integration.js.JavaScriptIntegrationLibraryImplementation I {  "array": [    {       "activation_aging": 60,       "api_temp_path": "\/opt\/lampp\/htdocs\/advanced\/api\/web\/temp\/",       "backend_temp_path": "\/opt\/lampp\/htdocs\/advanced\/backend\/web\/temp\/",       "cc_number": "1500599",       "common_id": 1,       "credit_interest_rate": "11.20",       "enable_otr_new": 1,       "enable_otr_owned": 1,       "env_info": "DEV",       "insurance_interest_rate": 20,       "is_contract_mandatory": 1,       "min_dp_percentage": 20,       "otr_threshold": 10000000,       "radius_length": 10,       "spa_words": "cumigalak",       "token_aging": 10    }  ],  "isSuccessful": true,  "responseHeaders": {    "Connection": "Keep-Alive",    "Content-Length": "625",    "Content-Type": "text\/html; charset=UTF-8",    "Date": "Tue, 12 Jul 2016 09:59:38 GMT",    "Keep-Alive": "timeout=5, max=99",    "Server": "Apache\/2.4.17 (Unix) OpenSSL\/1.0.1p mod_perl\/2.0.8-dev Perl\/v5.16.3",    "X-Powered-By": "Nintriva <nintriva.com>"  },  "responseTime": 46,  "statusCode": 200,  "statusReason": "OK",  "totalTime": 47  } [project worklight_server]    [7/12/16 17:00:12:800 WIB] 00000144  something line 1  something line 2  [project worklight_server]  


Solution:1

Apparently your record separator (RS) is [project worklight_server]

So try:

... | awk -v RS=".project worklight_server."  '/00000144/'   


Solution:2

If you know the value of X (number of lines you want) or if an approximation will do, you can put this after your tail

| grep -A 25 00000144  

which will give you any line with 00000144 and the next 25 lines.

If you don't know X but you can mark where the output should stop you could use sed for example. To get from a line with the PID to a line with 'project worklight_server':

| sed -n '/00000144/,/project\ worklight_server/p'  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »