grep¶
Search file contents for lines matching a regular expression.
Category: Read-only
Parameters¶
pattern(required)- Java regex pattern (
java.util.regexsyntax) to search for in file contents. path(optional)- The directory to search. Defaults to the current working directory.
include(optional)-
Java glob pattern restricting which files are searched (e.g.
*.kt,**/*.json). Uses the same syntax asglob. If omitted, all files in the search root are searched.Note
**/*.extrequires at least one directory separator before the filename. Use*.extto match files directly in the search root.
Output¶
Matches grouped by file, with each file sorted by modification time (most recent first) and matches within each file listed by line number:
Found 3 matches
/path/to/Foo.kt:
Line 12: val name: String
Line 45: fun name(): String
/path/to/Bar.kt:
Line 7: val name = "default"
Returns No matches found if nothing matches. Unreadable and binary files are silently skipped. Results are capped at 100 matches by default (configurable via toolLimits.grepMaxMatches in knosh.json); a truncation note is appended when the limit is reached. Lines longer than 2000 characters are truncated for display.
Limits¶
To keep a pathological caller-supplied pattern (e.g. catastrophic backtracking) from hanging the search, grep enforces two additional bounds:
- Time budget — the whole search has a wall-clock budget,
5000ms by default (configurable viatoolLimits.grepTimeoutMillis). If the budget is exceeded, the search stops and returns whatever matches were collected before the deadline, followed by a note (e.g.(Search stopped after 5000ms time budget; results may be incomplete.)). If the budget is exceeded before any matches are found, this note is returned instead ofNo matches found, so a timeout is never mistaken for a genuine absence of matches. - Line-length cap — each line is matched against only its first
100000characters by default (configurable viatoolLimits.grepMaxLineLength); a match starting beyond the cap is not found. This is independent of the 2000-character display truncation above: a line between 2000 andgrepMaxLineLengthcharacters is fully searched, but its displayed match text is still clipped at 2000 characters.