A NOTE has been added to this issue. ====================================================================== https://austingroupbugs.net/view.php?id=1941 ====================================================================== Reported By: dwheeler Assigned To: ajosey ====================================================================== Project: 1003.1(2024)/Issue8 Issue ID: 1941 Category: Shell and Utilities Type: Enhancement Request Severity: Objection Priority: normal Status: Under Review Name: David A. Wheeler Organization: User Reference: Section: grep Page Number: 1 Line Number: 1 Interp Status: --- Final Accepted Text: ====================================================================== Date Submitted: 2025-08-30 21:51 UTC Last Modified: 2025-09-20 02:32 UTC ====================================================================== Summary: Add widely-implemented options to grep ======================================================================
---------------------------------------------------------------------- (0007272) dwheeler (reporter) - 2025-09-20 02:32 https://austingroupbugs.net/view.php?id=1941#c7272 ---------------------------------------------------------------------- <p>As requested, I've tried to rewrite this to a single proposed change. I tried to make it sound "POSIX-like". Improvements welcome.</p> <p><b>Desired Action</b></p> <p>Update the grep utility specification to add the following widely-implemented options that improve grep's functionality across systems, specifically -A -B -C -G -H -L -h -m -o with majority semantics, extending the existing POSIX grep specification here: https://pubs.opengroup.org/onlinepubs/9799919799/utilities/grep.html</p> <p> This is the proposed resolution for https://austingroupbugs.net/view.php?id=1941 </p> <p><b>Synopsis Changes:</b></p> <p>Modify the synopsis to include the new options:</p> <pre> grep [-E|-F|-G] [-c|-l|-L|-q] [-H|-h] [-inosvx] [-A num] [-B num] [-C num] [-m num] -e pattern_list [-e pattern_list]... [-f pattern_file]... [file...] grep [-E|-F|-G] [-c|-l|-L|-q] [-H|-h] [-inosvx] [-A num] [-B num] [-C num] [-m num] [-e pattern_list]... -f pattern_file [-f pattern_file]... [file...] grep [-E|-F|-G] [-c|-l|-L|-q] [-H|-h] [-inosvx] [-A num] [-B num] [-C num] [-m num] pattern [file...] </pre> <p><b>Option Descriptions:</b></p> <p>Add the following option descriptions to the OPTIONS section:</p> <p><b>-A</b> <i>num</i></p> <p>Write <i>num</i> lines of context after each selected line.</p> <p><b>-B</b> <i>num</i></p> <p>Write <i>num</i> lines of context before each selected line.</p> <p><b>-C</b> <i>num</i></p> <p>Write <i>num</i> lines of context before and after each selected line. Equivalent to specifying both -A <i>num</i> and -B <i>num</i>.</p> <p><b>-G</b></p> <p>Interpret patterns as basic regular expressions. This is the default behavior.</p> <p><b>-H</b></p> <p>Prefix each output line with the filename followed by a colon (or dash if it is a context line and is not selected). Default when multiple files are specified.</p> <p><b>-L</b></p> <p>Write only names of files containing no selected lines, one per line. Empty files are included if processed. If standard input was processed its name is considered "(standard input)". If the same filename was processed multiple times, and it is to be output, it's implementation-defined whether its name will be output only once or once for each occurance.</p> <p><b>-h</b></p> <p>Suppress filename prefixes. Default when reading standard input or a single file.</p> <p><b>-m</b> <i>num</i></p> <p>Stop reading each file for matching after <i>num</i> selected lines. Complete context for the last match should be written after stopping, but any lines written afterwards are not considered matches. Implementations may instead stop writing lines for that file after writing the corresponding information about the last match, without the trailing context. This is applied independently to each file. <i>num</i> shall be a non-negative decimal integer; if zero, behavior is implementation-defined.</p> <p><b>-o</b></p> <p>Write only matching non-empty portions of selected lines, possibly prefixed with filename and line number information (see STDOUT). Multiple non-overlapping matches for one pattern are written as separate lines, with the same filename and/or line number if they are written. Combining -o with context options (-A, -B, -C) or -v is implementation-defined.</p> <p>For context options (-A, -B, -C), if there are overlapping or adjacent regions of context, the lines are written in sequence without duplication, and the group separator ('--') is written to separate non-adjacent context regions. When writing a filename and/or line number, non-matching context lines use dash separators while selected lines use colon separators (see STDOUT section). If a context line also matches the pattern, it shall be displayed as a selected line and counted toward the -m limit. A context does not extend before the beginning or after the end of a file. For all numeric arguments, <i>num</i> shall be a non-negative decimal integer.</p> <p><b>STDOUT Changes:</b></p> <p>If -q is specified, there is no output to standard out regardless of other options.</p> <p>Otherwise, if the -l or -L option is in effect, the following shall be written for each file containing at least one selected input line:</p> "%s\n", <file> <p>Otherwise, if the -H option is selected or more than one file argument appears (and -q, -l, and -L are not specified), the grep utility shall prefix each output line by this, where <context-marker> is ':' if this line is selected and '-' if this line was not selected but is instead only being displayed as a context line: "%s%s", <file>, <context-marker> The remainder of each output line shall depend on the other options specified: If the -c option is in effect, the remainder of each output line shall contain: "%d\n", <count> Otherwise, if -c is not in effect and the -n option is in effect, the following shall be written to standard output, where <context-marker> is the same as described above: "%d%s", <line number>, <context-marker> Finally, if the '-o' option was selected, the following shall be written to standard output, with multiple lines for each selected-part that matches: "%s\n", <selected-part> Otherwise, if the -o option was not selected, the following shall be written to standard output, which may either be a selected line, or one of the context lines before or after a selected line: "%s\n", <line contents> <p>Context group separators shall consist of a single line containing only <b>"--"</b> when context from different matches would otherwise be adjacent and there is at least one non-context line between the context regions in the original file.</p> <p><b>Option Interactions:</b></p> <ul> <li>Context options -A and -B may be combined; the result is the union of the specified ranges.</li> <li>The impact of -C combined with -A and/or -B is implementation-defined; implementations are encouraged to use left-to-right application where last one(s) given take precedence.</li> <li>Pattern interpretation options (-E, -F, -G), filename options (-H, -h), and output format options (-l, -L, -o) are mutually exclusive within each group; the last specified takes effect.</li> <li>Context and -o options have no effect with -c, -l, -L, or -q.</li> <li>Context options with -v display context around non-matching selected lines.</li> <li>Binary file treatment is implementation-defined.</li> </ul> <p><b>Error Conditions:</b></p> <p>Invalid numeric arguments shall cause grep to write a diagnostic message to standard error and exit with status greater than 1.</p> Issue History Date Modified Username Field Change ====================================================================== 2025-08-30 21:51 dwheeler New Issue 2025-08-30 21:51 dwheeler Status New => Under Review 2025-08-30 21:51 dwheeler Assigned To => ajosey 2025-08-30 21:56 dwheeler Note Added: 0007240 2025-08-30 21:59 dwheeler Note Added: 0007241 2025-08-31 00:07 mirabilos Note Added: 0007242 2025-08-31 00:10 mirabilos Note Added: 0007243 2025-08-31 21:52 dwheeler Note Added: 0007244 2025-08-31 22:01 dwheeler Note Added: 0007245 2025-09-01 05:57 stephane Note Added: 0007246 2025-09-01 06:05 stephane Note Added: 0007247 2025-09-01 15:36 dwheeler Note Added: 0007249 2025-09-01 17:10 dwheeler Note Added: 0007250 2025-09-01 17:18 dwheeler Note Added: 0007251 2025-09-11 15:31 lanodan Note Added: 0007253 2025-09-11 15:36 lanodan Note Edited: 0007253 2025-09-11 15:37 lanodan Note Edited: 0007253 2025-09-11 15:37 lanodan Note Edited: 0007253 2025-09-11 15:50 geoffclare Project 1003.1(2008)/Issue 7 => 1003.1(2024)/Issue8 2025-09-11 18:01 dwheeler Note Added: 0007256 2025-09-12 16:28 stephane Note Added: 0007258 2025-09-15 02:03 dwheeler Note Added: 0007259 2025-09-15 02:04 dwheeler Note Edited: 0007259 2025-09-15 02:08 dwheeler Note Edited: 0007259 2025-09-15 09:14 stephane Note Added: 0007260 2025-09-16 13:47 dwheeler Note Added: 0007261 2025-09-16 16:55 collinfunk Note Added: 0007262 2025-09-17 00:06 mirabilos Note Added: 0007263 2025-09-17 07:29 hvd Note Added: 0007264 2025-09-17 14:17 dwheeler Note Added: 0007265 2025-09-17 23:47 mirabilos Note Added: 0007266 2025-09-18 15:26 dwheeler Note Added: 0007267 2025-09-18 16:14 geoffclare Note Added: 0007269 2025-09-18 16:14 geoffclare Note Edited: 0007269 2025-09-18 18:02 dwheeler Note Added: 0007271 2025-09-20 02:32 dwheeler Note Added: 0007272 ======================================================================
