Using lookbehinds with grep (ggrep for MacOs)
When working with text data on the command line, lookbehinds can be a valuable took, especially for extracting specific fields from JSON or logs. While macOS comes with BSD grep (which doesn’t support advanced regex), we can use GNU grep with PCRE (Perl-Compatible Regular Expressions) to access features like lookbehinds by installing ggrep using brew install grep.
Using the following JSON:
And typing:
Will print:
• The (?<=…) syntax is a lookbehind that ensures the match only occurs after “name”:. • -o makes it print just the match, so you get a clean result with no extra text. • -P stands for Perl-Compatible Regular Expressions (PCRE). This flag enables more advanced regex features—such as lookbehinds, lookaheads, and non-capturing groups—that are part of the Perl regex syntax but not available in standard grep.
You can also use SED and AWK in Linux for similar results but this, personally has the simplest syntax.
What commands do you use daily to make your life easier?