BSD grep -m with -A or -C stops prematurely

Originator:raimue
Number:rdar://13189776 Date Originated:11-Feb-2013 04:06 PM
Status:Duplicate/11129333 Resolved:13-Feb-2013 01:25 AM
Product:Mac OS X Product Version:10.8.2/12C3006
Classification:UI/Usability Reproducible:Always
 
11-Feb-2013 04:06 PM Rainer Müller:
Summary:

The implementation of BSD grep in /usr/bin/grep does not handle the combination of the -m with one of the -A/-C flags correctly. Instead of printing the after context for the last match, it exits prematurely.

Steps to Reproduce:

Use /usr/bin/grep with arguments -A or -C which are meant to output context after or around the match and additionally use -m to limit the total number of matches. For the last match hitting the given limit no after context is printed.

Here is a comparison of the behavior with GNU grep installed from MacPorts:

$ /usr/bin/grep --version
grep (BSD grep) 2.5.1-FreeBSD
$ /opt/local/bin/grep --version |head -n1
/opt/local/bin/grep (GNU grep) 2.14
$ echo -e "Foo\nBar\nBaz\n" | /usr/bin/grep -m1 -A2 Foo
Foo
$ echo -e "Foo\nBar\nBaz\n" | /opt/local/bin/grep -m1 -A2 Foo
Foo
Bar
Baz
$ echo -e "Foo\nBar\nBaz\n" | /usr/bin/grep -m1 -C1 Bar
Foo
Bar
$ echo -e "Foo\nBar\nBaz\n" | /opt/local/bin/grep -m1 -C1 Bar
Foo
Bar
Baz
$

Expected Results:

BSD grep should return the same result as GNU grep in the examples above.

Actual Results:

The context after the match is missing from the output.

Regression:

This is a regression as the behavior of /usr/bin/grep is different to the behavior of GNU grep, which was shipped in previous versions of Mac OS X.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!