grep system ‘grep’ command
Overview
The grep command searches for lines matching a regular expression in
text files (or standard input). It uses the NuttX libc regular expression
library (regcomp / regexec), so it supports real regular
expressions including anchors (^, $) and character classes
([...]), not just fixed substring matching.
By default grep reads from stdin when no file is given, which
makes it useful in NSH pipelines. When more than one file is searched (or
when recursing into directories), the matching file name is prefixed to
each result line.
Configuration
Enable the command with CONFIG_SYSTEM_GREP. This option depends on
CONFIG_LIBC_REGEX and is disabled by default.
The following configuration options are available:
CONFIG_SYSTEM_GREP_PROGNAMEProgram name for the
grepcommand. Default:grep.CONFIG_SYSTEM_GREP_PRIORITYTask priority for the
grepcommand. Default: 100.CONFIG_SYSTEM_GREP_STACKSIZEStack size for the
grepcommand. Default:DEFAULT_TASK_STACKSIZE.
Usage
grep [-invrHh] <pattern> [<file>...]
When no <file> is given, grep reads from stdin.
Options
-iCase-insensitive match.
-nPrefix each matching line with its 1-based line number.
-vInvert the match: select lines that do not match.
-rRecurse into subdirectories. When set, the file name is always prefixed to each result.
-HAlways print the file name prefix, even with a single input file.
-hNever print the file name prefix.
Exit status
0At least one line matched.
1No line matched.
2An error occurred (invalid option, unreadable file, bad regex, …).
Examples
Search stdin for lines containing error:
nsh> dmesg | grep error
Case-insensitive search with line numbers:
nsh> grep -in error /tmp/log.txt
Invert the match (print non-matching lines):
nsh> grep -v error /tmp/log.txt
Regular expression anchor – match lines starting with ERR:
nsh> grep "^ERR" /tmp/log.txt
Recurse a directory, always printing the file name:
nsh> grep -r -H TODO /data/src
Pipe usage – filter the output of another NSH command:
nsh> ps | grep work
Notes
grepis built on the libc regex library;CONFIG_SYSTEM_GREPdepends onCONFIG_LIBC_REGEX, so both must be enabled to satisfy theregcomp/regexecdependency.The
-roption implies multi-file mode, so the file name is always prefixed regardless of-H/-h.Long lines are truncated at
GREP_LINE_MAX(512 bytes); matching is performed on the (newline-stripped) content of each line.