In DE_Tools see: https://github.com/rhyslwells/DE_Tools/blob/main/DE_Tools/Explorations/Other/Terminal/Grep

Example of what Grep can do

  1. Search for a pattern in files
grep "error" logfile.txt
  • Searches for the word "error" in logfile.txt.
  • Useful for quickly checking logs or documents.
  1. Recursive search in directories
grep -r "TODO" ./src
  • Recursively searches all files in the ./src directory for "TODO".
  • Useful for finding code comments, notes, or markers across a codebase.
  1. Display line numbers of matches
grep -n "fail" test_results.txt
  • Shows line numbers where "fail" appears.
  • Helpful for debugging or jumping to the right place in a large file.
  1. Show only matching part of the line
grep -o "http[s]\?://[a-zA-Z0-9./?=_-]*" webpage.html
  • -o prints only the matching substring (e.g., all URLs from HTML).
  • Good for extracting emails, URLs, etc.
  1. Search multiple files and include filenames
grep "password" *.conf
  • Searches all .conf files and prints matching lines with their filenames.
  • Useful for auditing sensitive configurations.

Flags

Useful for finding where a function is used in a code base.

FlagDescription
-wMatch whole words only (ignores substrings).
-iPerform a case-insensitive match.
-nShow line numbers of matching lines.
-rRecursively search subdirectories.
-lList only filenames that contain matches (no line content shown).
-cCount matches per file, instead of showing them.
-A NShow N lines after each match.
-B NShow N lines before each match.
-C NShow N lines before and after each match (context lines).
-PUse Perl-compatible regular expressions (PCRE).
-vInvert match: show lines that do not match the pattern.
-oPrint only the matching part of the line (not the entire line).
--colorHighlight matched strings with color (if supported by terminal).
--includeInclude only files that match the given glob pattern (used with -r).
--excludeExclude files that match the given pattern from recursive searches.