BusyBox Bug and Patch Tracking
BusyBox
  

Viewing Issue Simple Details Jump to Notes ] View Advanced ] Issue History ] Print ]
ID Category Severity Reproducibility Date Submitted Last Update
0000999 [BusyBox] New Features feature N/A 08-16-06 15:53 09-30-06 13:21
Reporter jac_goudsmit View Status public  
Assigned To BusyBox
Priority normal Resolution fixed  
Status closed   Product Version 1.2.x
Summary 0000999: [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".
Additional Information What changed:

- Added 'o' to GREP_OPTS, added GREP_OPT_o macro to correspond to it and modified all other GREP_OPT_? macros to accomodate for the change
- Added declaration of regmatch_t regmatch to grep_file(), modified the call to regexec so it uses regmatch; regmatch's contents have to be reset before _each_ regexec call because regexec doesn't change regmatch if there's no match, this would give trouble when using -v option.
- Put an "if" around the print_line call in grep_file() so when -o is used, the program only prints the part of the line that matches the (first occurrence of the) regular expression.
- Modified initialization of reflags static variable so that the REG_NOSUB flag is not set when -o is used (when REG_NOSUB is set, regexec ignores the regmatch parameters).

Use the patch in any way you find useful. I will be happy to help if you find a problem. The patch doesn't include a change to the documentation or test code. Let me know if you need help with that. I did some brief testing on various combinations of -o with other options but my testing was not exhaustive; if you find any anomalies and want me to fix it, let me know.

Thanks for reading!
Attached Files  grep-o.patch [^] (3,015 bytes) 08-16-06 15:53

- Relationships

- Notes
(0001625)
vda
09-07-06 10:22

It would be nice to match GNU grep, tho...
 
(0001627)
jac_goudsmit
09-07-06 11:55
edited on: 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).

 
(0001654)
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


Copyright © 2000 - 2006 Mantis Group
Powered by Mantis Bugtracker