AJC Grep Commands Cheat Sheet: Common Examples and OptionsAJC Grep is a powerful command-line utility designed for searching plain-text data for lines that match patterns. It builds on the familiar concepts of traditional grep tools while adding features and defaults intended to speed up common developer and sysadmin workflows. This cheat sheet covers the most useful AJC Grep commands, common examples, and practical options you’ll reach for when working with logs, code, or large text files.
Quick reference: basic invocation
- Basic search:
ajc-grep "pattern" file.txt
- Recursive search:
ajc-grep -r "pattern" /path/to/dir
- Case-insensitive:
ajc-grep -i "pattern" file.txt
- Show line numbers:
ajc-grep -n "pattern" file.txt
Common options explained
-
-r
/--recursive
Search directories recursively. Equivalent to walking the directory tree and searching each file that matches file filters. -
-i
/--ignore-case
Perform case-insensitive matching. -
-n
/--line-number
Prefix matching lines with their line numbers. -
-v
/--invert-match
Select lines that do not match the pattern. -
-c
/--count
Print only a count of matching lines per input file. -
-l
/--files-with-matches
Print only the names of files with at least one matching line. -
-L
/--files-without-match
Print only the names of files with no matching lines. -
-H
/--with-filename
Always print file name headers with matching lines (useful when searching a single file but you still want a filename prefix). -
-o
/--only-matching
Show only the parts of a line that match the pattern (useful for extracting tokens). -
-A NUM
/--after-context=NUM
Print NUM lines of trailing context after matching lines. -
-B NUM
/--before-context=NUM
Print NUM lines of leading context before matching lines. -
-C NUM
/--context=NUM
Print NUM lines of output context both before and after each matching line. -
-E
/--extended-regexp
Use extended regular expressions (EREs) for the pattern syntax. -
-F
/--fixed-strings
Treat pattern as a fixed string (literal), not a regex — faster for simple substring searches. -
--exclude=GLOB
/--include=GLOB
Exclude or include files matching shell-style globs when doing recursive searches. -
--binary-files=TYPE
Control behavior with binary files (binary
,text
,skip
).
Practical examples
Search for an error string in logs (case-insensitive), showing file and line numbers:
ajc-grep -rni "error connecting to database" /var/log/myapp
Count occurrences of a pattern in each file:
ajc-grep -c "timeout" *.log
Find files that do not contain the word “SUCCESS”:
ajc-grep -L "SUCCESS" /path/to/reports/*.txt
Extract only matching email addresses from text:
ajc-grep -o -E "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,}" bigfile.txt
Show matches with 3 lines of context before and after:
ajc-grep -C 3 "stack trace" app.log
Search recursively but exclude node_modules and .git directories:
ajc-grep -r --exclude="node_modules/*" --exclude=".git/*" "TODO" .
Match literal strings (fast) — useful when searching for a short fixed token:
ajc-grep -F "TODO: refactor" src/**/*.js
Invert match to show only non-matching lines (useful for filtering out noise):
ajc-grep -v "DEBUG" application.log
Show only filenames that contain a match and suppress the matching lines:
ajc-grep -l "CRITICAL" /var/log/*
Handling files with mixed binary/text content (force text processing):
ajc-grep --binary-files=text "partial data" dump.bin
Performance tips
- Use
-F
for simple substring searches to avoid regex overhead. - Limit search scope with
--include
/--exclude
globs to reduce I/O. - When searching many files, use
-l
first to find candidate files, then run a detailed per-file search. - Pipe large outputs to
less -R
or redirect to a file rather than printing to the terminal.
Combining AJC Grep with other commands
- Count matches across all files:
ajc-grep -r "fail" . | wc -l
- Show unique matching lines sorted:
ajc-grep -h "WARNING" logs/*.log | sort | uniq
- Use with xargs to process matching files:
ajc-grep -rl "migration_needed" . | xargs -r sed -n '1,200p'
Regex examples
- Match IPv4 addresses:
ajc-grep -E -o "([0-9]{1,3}.){3}[0-9]{1,3}" server.log
- Capture timestamps like 2025-08-31 14:05:12:
ajc-grep -E -o "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}" app.log
- Match lines that start with ERROR:
ajc-grep -E "^ERROR" /var/log/app.log
Exit codes
- 0 — At least one match found.
- 1 — No matches found.
- 2 — An error occurred (invalid arguments, I/O error).
Troubleshooting
- If colors or highlighting look wrong, check
TERM
and related environment variables. - If recursion seems slow, verify you’re not traversing large binary directories (use
--exclude
). - For unexpected behavior with patterns, try
-F
to confirm whether regex interpretation is the issue.
This cheat sheet covers the most commonly used AJC Grep commands and options for routine text-search tasks. Keep this as a reference while working with logs, codebases, or any large text datasets—small flags yield big efficiency gains.