Grep Command Builder

Build a grep command without remembering which of -E, -F, -P and -G you need, or why --include sometimes does nothing. Type the text or pattern you are looking for, choose how grep should read it (basic, extended, fixed string or PCRE), decide whether case matters, point it at a file or a directory tree, and pick the output you want: matching lines with context, line numbers, a count, or just the list of files that contain a match. The command is assembled as you type, every flag is explained in plain English, and a checks panel catches the mistakes that quietly return the wrong results — a \d in a basic regex, -o combined with -v, context options that a summary flag silently ignores. The pattern tester below runs the same pattern against text you paste or load, highlighting every match line by line, so you can confirm it matches what you think before you touch a terminal. Everything runs in your browser, nothing is uploaded, and no command is executed here.

100% private. The builder, the pattern tester and the generated script are all created with JavaScript in this tab. Patterns, filenames and any file you load are read with the browser’s own FileReader API — they are never sent to this website or to anyone else, and nothing is ever executed — you review the command and run it yourself.

Quick starts

1. What to search for

Case

2. Where to search

3. What to print

Generated command

Ready

Checks on this command

What each part does

PartPlain English

Test the pattern on your own text

Paste a log file, a config snippet or a block of source and see exactly which lines match, with every hit highlighted. This runs the pattern from step 1 through the browser’s regex engine — it is an approximation of GNU grep (the differences are noted below), but it catches the expensive mistakes: a pattern that matches nothing, or one that matches every line.

Files are read with FileReader in this tab — nothing is uploaded.

Browser regex flavour: JavaScript, which is close to -E and (for the basics) to -P. Lookarounds and \d work here and in -P, but not in basic or extended mode; POSIX bracket expressions such as [[:digit:]] work in grep but not here. -w and -x are approximated the way grep applies them to each line.

Paste an existing grep command

Drop a command you already have (from a script, a man page or your shell history) and the form is filled in from it. Anything the form cannot represent is listed so you know what will be dropped.

or

A loaded file is read with the browser’s FileReader — it is never uploaded.

How it works

grep reads each line of its input and prints the ones that match a pattern. That is the whole idea, and almost every surprise comes from two details: which regex dialect is in use, and what grep does when you point it at a directory. By default grep reads a basic regular expression (BRE), in which + ? | ( ) { } are ordinary characters. -E switches to extended regular expressions, where they become operators — which is why grep 'a|b' silently searches for the literal text a|b while grep -E 'a|b' searches for either letter. This builder defaults to -E for that reason.

Recursion is the other trap. grep pattern dir/ does not search inside dir, it just prints grep: dir: Is a directory; you need -r. Once you have -r, the --include, --exclude and --exclude-dir filters start working — they are ignored without it, which is the single most common reason a “filtered” grep returns everything. --include and --exclude match the file name (GNU grep also matches the path), so --include='*.py' is right and --include='src/*.py' is not portable.

Context comes from -A (after), -B (before) and -C (both). They are display options only, and grep ignores them the moment you ask for a summary instead of lines: with -c, -l, -L, -q or -o no context is printed. When several matches are close together their context blocks are merged and separated by --, so the output stays valid to pipe onwards.

Patterns are always single-quoted here. Unquoted, the shell expands * against your current directory and expands $VAR before grep ever sees it — the classic symptom is a pattern that works in one directory and matches nothing in another. Single quotes stop both; the only character that needs care is a literal apostrophe, which the builder escapes for you.

grep dialects and what changes between them

ModeFlagWhat it means in practice
Basic (BRE)none (or -G)The historical default. + ? | ( ) { } are literals; write \+, \?, \|, \( for the operators. No \d or \w — \d matches a letter d.
Extended (ERE)-E / egrepOperators work unescaped: a|b, colou?r, [0-9]{2,4}. Still no \d or lookarounds — GNU treats \d as a plain d.
Fixed string-F / fgrepEvery character is literal. Plus -w or -x when you want word or line boundaries. Fastest option for a literal search.
Perl (PCRE)-P\d \w \s, lookahead and lookbehind. GNU grep only — the BSD grep shipped with macOS rejects -P, and it is slow on large trees.

The options this builder can emit

OptionWhat it doesGotcha
-iIgnore case in the pattern.Also folds character classes, so [a-z] matches uppercase.
-vInvert: print lines that do not match.With -o nothing is printed at all.
-w / -xMatch whole words / whole lines.-w still allows a match inside a longer word with punctuation.
-r / -RRecurse into directories; -R also follows symlinks.Without one of them, a directory argument is an error, not a search.
--include / --excludeFilter files by name glob.Ignored without recursion. Matches the basename.
--exclude-dirPrune whole directories.Also a large speed win on repositories.
-A / -B / -CPrint N lines after / before / around each hit.No effect with -c, -l, -L, -q or -o.
-nPrefix each line with its line number.Ignored by -l and -c, which print no lines.
-oPrint only the matched text, one per line.Implies you lose the rest of the line; -c then still counts lines, not hits.
-c / -l / -LCount matching lines, list matching files, list non-matching files.They replace the normal output entirely, so -n and context do nothing.
-m NStop reading a file after N matching lines.-m 0 matches nothing and exits 1.
-I / -aSkip binary files / treat them as text.grep’s default is to print “Binary file … matches” and stop.
--color=alwaysHighlight the matched text.always keeps the colour through a pipe; use auto for terminals.

Exit codes, and why grep “fails” in scripts

StatusMeaning
0At least one line matched.
1No match at all — this is a normal, successful search that found nothing.
2A real error: a file was missing, a directory was given without -r, or the regex was invalid.

Under set -e, or with && chains, status 1 aborts a script even though nothing broke. Guard it with if grep -q pattern file; then, or append || true. Likewise -q is the right way to test for a match: it stops at the first hit and prints nothing.

Troubleshooting

SymptomCause and fix
grep: dir: Is a directoryAdd -r. grep does not search directories by default.
Binary file … matchesThe tree contains an object file or image. Add -I, or restrict with --include.
Invalid regular expressionUnbalanced [ or (, or a backslash before an ordinary character in BRE. Switch to -E and fix the grouping.
grep -P: invalid option …BSD grep on macOS. Use ggrep -P from Homebrew coreutils, or rewrite with -E.
Pattern matches nothing that obviously existsUnquoted pattern expanded by the shell, or * taken from your working directory. Quote it.
--include does nothingRecursion is off. Add -r.
Permission denied floodSearching / as a normal user. Narrow the path, or add -s to silence the messages (the builder leaves that to you).
Output is hugeAdd -l for the file list, -c for counts, or -m 1 per file while you are exploring.

Luke Keam
Spot a bug or want a custom utility built? I'd love to hear from you. Get in touch
Luke Keam | techgeek.biz