Skip to content

Instantly share code, notes, and snippets.

@pmenke-de
Last active January 31, 2025 07:30
Show Gist options
  • Save pmenke-de/4060213f15b6b634225f92b2ba5c26d5 to your computer and use it in GitHub Desktop.
Save pmenke-de/4060213f15b6b634225f92b2ba5c26d5 to your computer and use it in GitHub Desktop.
Extract json documents using RegEx

Taken from this amazing answer and condensed to a one-line grep:

grep -Po '(?(DEFINE)(?<ws>[\t\n\r ]*)(?<number>-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[Ee][+-]?\d++)?)(?<boolean>true|false|null)(?<string>"(?:[^\\"\x00-\x1f]|\\["\\bfnrt\/]|\\u[0-9A-Fa-f]{4})*")(?<pair>(?&ws)(?&string)(?&ws):(?&value))(?<array>\[(?:(?&value)(?:,(?&value))*)?(?&ws)\])(?<object>\{(?:(?&pair)(?:,(?&pair))*)?(?&ws)\})(?<value>(?&ws)(?:(?&number)|(?&boolean)|(?&string)|(?&array)|(?&object))(?&ws)))(?&object)'

Useful for grepping mixed logs, extrating JSON and then processing it with jq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment