[BusyBox 0000999]: [PATCH] grep option -o

bugs at busybox.net bugs at busybox.net
Sat Sep 30 20:21:40 UTC 2006


The following issue has been CLOSED 
====================================================================== 
http://busybox.net/bugs/view.php?id=999 
====================================================================== 
Reported By:                jac_goudsmit
Assigned To:                BusyBox
====================================================================== 
Project:                    BusyBox
Issue ID:                   999
Category:                   New Features
Reproducibility:            N/A
Severity:                   feature
Priority:                   normal
Status:                     closed
Resolution:                 fixed
Fixed in Version:           1.2.x
====================================================================== 
Date Submitted:             08-16-2006 15:53 PDT
Last Modified:              09-30-2006 13:21 PDT
====================================================================== 
Summary:                    [PATCH] grep option -o
Description: 
Here's a patch to findutils/grep.c (based on the 1.2.1 release) that
implements the -o option ("print pattern only"). 

For example, the following command lists all the options in the kernel
config file:

  busybox grep -o "CONFIG_[^=]*" /usr/src/linux/.config

I did some brief testing and found one issue compared to GNU grep (2.5.1):
when -o is used in combination with -v, the modified busybox grep will
print empty lines whereas GNU grep prints nothing. When -o is used with -v
and -n, the modified busybox grep prints line numbers followed by line
feeds, whereas GNU grep prints line numbers in a long stream without line
feeds. However the usage of -o in combination with -v is so unlikely that
I don't think it's worth fixing.

For example: when running this command:

  grep -ov CONFIG /usr/src/linux/.config

GNU grep will produce no output, busybox produces a lot of empty lines.
Another example: when running this command:

  grep -ovn CONFIG /usr/src/linux/.config

GNU grep will produce a long list of line numbers in one stream:

  1:2:3:4:5: ... (etc)

Busybox produces a lot of empty lines with line numbers:

  1:
  2:
  3:
  4:
  5:
  ... (etc)

A pretty simple patch that has helped me in my project which is to build a
uClibc/BusyBox-based install of Gentoo. Gentoo's rc scripts use grep -o in
a lot of places and it was really a lot easier to modify the grep.c code
than to manually replace all the occurrences of grep -o by something like
sed -n "s/something\(${1}\)something/\1/gp".

====================================================================== 

---------------------------------------------------------------------- 
 vda - 09-07-06 10:22  
---------------------------------------------------------------------- 
It would be nice to match GNU grep, tho... 

---------------------------------------------------------------------- 
 jac_goudsmit - 09-07-06 11:56  
---------------------------------------------------------------------- 
@vda:

Nice, yes. 

But the point I was trying to make was:
- Using sed to emulate the grep -o option in scripts is a pain in the butt
and is hard to automate -- it's not just a matter of searching all your
scripts for grep -.*o.* regexp and replacing it with sed
's/.*\(regexp\).*/\1/g', this won't work e.g. when regexp contains ^ or $
- Implementing grep -o option is relatively easy for the general case, and
doesn't add much code to Busybox
- Output _is_ equal to GNU grep in the cases I tested; The only difference
with GNU grep appears to be when both -o and -v are used
- Using -o and -v at the same time doesn't make sense in most normal
cases
- I think GNU grep (not Busybox) is in error in the -o -v corner case, or
at least in the -o -v -n case (arguably).

 

---------------------------------------------------------------------- 
 vda - 09-29-06 14:11  
---------------------------------------------------------------------- 
Applied as rev 16261 

Issue History 
Date Modified   Username       Field                    Change               
====================================================================== 
08-16-06 15:53  jac_goudsmit   New Issue                                    
08-16-06 15:53  jac_goudsmit   Status                   new => assigned     
08-16-06 15:53  jac_goudsmit   Assigned To               => BusyBox         
08-16-06 15:53  jac_goudsmit   File Added: grep-o.patch                     
09-07-06 10:22  vda            Note Added: 0001625                          
09-07-06 11:55  jac_goudsmit   Note Added: 0001627                          
09-07-06 11:56  jac_goudsmit   Note Edited: 0001627                         
09-29-06 14:11  vda            Status                   assigned => resolved
09-29-06 14:11  vda            Fixed in Version          => 1.2.x           
09-29-06 14:11  vda            Resolution               open => fixed       
09-29-06 14:11  vda            Note Added: 0001654                          
09-30-06 13:21  vda            Status                   resolved => closed  
======================================================================




More information about the busybox-cvs mailing list