Anonymous | Login | Signup for a new account | 11-10-2008 11:05 PST |
Main | My View | View Issues | Change Log | Docs |
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 | ||||||||
|
Copyright © 2000 - 2006 Mantis Group |