Skip to content

Instantly share code, notes, and snippets.

@TommyPKeane
Created February 28, 2023 14:27
Show Gist options
  • Save TommyPKeane/765b30c23e90bd60e00c659d33e7f782 to your computer and use it in GitHub Desktop.
Save TommyPKeane/765b30c23e90bd60e00c659d33e7f782 to your computer and use it in GitHub Desktop.
Examples and Syntax Explanations for using `grep` to parse Files or Output Streams from other commands/operations in `bash` Syntax
# `grep` vs. `ggrep`
#
# Note that in macOS as a Unix-like System (BSD-based), a majority
# of the Unix Commandline Tools are already pre-installed and used
# as part of the Operating System (OS). This, however, causes the
# problem that many of these tools are version-locked for the intent
# of retaining compatibility within the OS itself, meaning that they
# are often old/out-of-date. As such, we can use `brew` (Homebrew)
# in macOS to install the latest versions of these utilities. But,
# when using the latest GNU variant of a critical OS utility (like
# `grep`, `bash`, `make`, etc.) `brew` will create symlinks prefaced
# with `g` to indicate the post-installed GNU version instead of the
# innate pre-installed version that's used by macOS.
#
# So, to use the latest version of `grep` it's best to run:
# `brew install grep`
# Which will then be accessible by the command `ggrep`, while you
# (and the macOS internals) can still refer to the pre-installed
# older version by the command `grep`.
#
# For the purpose of leveraging the latest/current syntax
# functionality these examples all use `ggrep` to specify that these
# commands may not work in macOS with the pre-installed `grep`, and
# may in fact need the latest version of GNU `grep` to actually work.
# Extract Unique Domain Names (with Sub-domains) from a Plaintext
# Newline Delimited File (`filename.txt`). The piped output
# portion can be dropped if you don't care about uniqueness.
ggrep -Pio "(?<=:\/\/)[\w\.\-\d]+" /path/to/filename.txt | uniq -u
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment