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

bugs at busybox.net bugs at busybox.net
Thu Sep 7 18:55:21 UTC 2006

A NOTE has been added to this issue. 
Reported By:                jac_goudsmit
Assigned To:                BusyBox
Project:                    BusyBox
Issue ID:                   999
Category:                   New Features
Reproducibility:            N/A
Severity:                   feature
Priority:                   normal
Status:                     assigned
Date Submitted:             08-16-2006 15:53 PDT
Last Modified:              09-07-2006 11:55 PDT
Summary:                    [PATCH] grep option -o
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:

  ... (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:55  

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
- I think GNU grep (not Busybox) is in error in the -o -v corner case

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                          

More information about the busybox-cvs mailing list