I have a logfile I'm tailing and want to output only those yaml documents (separated by ---
) containing a specific string (specific domain in hostname).
Example logfile contents:
(focus on the hostname
)
---
event: outgoing HTTP response
timestamp: 2021-10-06T08:15:28.212Z
remoteAddress: "1.2.3.4"
hostname: a.b.c.domain.com
statusCode: 200
headers:
content-length: 524
etc: ...
body: "blabla (can be multiline and can contain anything)"
---
event: outgoing HTTP response
timestamp: 2021-10-06T08:15:28.212Z
remoteAddress: "1.2.3.4"
hostname: a.b.c.different.com
statusCode: 200
headers:
content-length: 524
etc: ...
body: "blabla (can be multiline and can contain anything)"
---
event: outgoing HTTP response
timestamp: 2021-10-06T08:15:28.212Z
remoteAddress: "1.2.3.4"
hostname: 1.2.3.domain.com
statusCode: 200
headers:
content-length: 524
etc: ...
body: "blabla (can be multiline and can contain anything)"
expected output:
---
event: outgoing HTTP response
timestamp: 2021-10-06T08:15:28.212Z
remoteAddress: "1.2.3.4"
hostname: a.b.c.domain.com
statusCode: 200
headers:
content-length: 524
etc: ...
body: "blabla (can be multiline and can contain anything)"
---
event: outgoing HTTP response
timestamp: 2021-10-06T08:15:28.212Z
remoteAddress: "1.2.3.4"
hostname: 1.2.3.domain.com
statusCode: 200
headers:
content-length: 524
etc: ...
body: "blabla (can be multiline and can contain anything)"
I cannot get my head around the regex I need. Matching every document (regardless of what's inside) I'm doing this:
/---\n[\s\S]+?(?=\n---|$)/g
see also: https://regex101.com/r/a8zKSz/2
However I cannot figure out how to only output those documents matching hostname with the domain domain.com
(regex for the match within could be e.g. /hostname: .*?domain\.com/
I like to end up having a sed / perl or any other "oneliner" applicable on a "default linux OS". tail -F logfile.log | oneliner
But getting the regex is the first step.
Any hints or help is appreciated.