This post originated from an RSS feed registered with Java Buzz
by Weiqi Gao.
Original Post: GNU grep's New Trick
Feed Title: Weiqi Gao's Weblog
Feed URL: http://www.weiqigao.com/blog/rss.xml
Feed Description: Sharing My Experience...
I stumbled upon this Linux.com article: GNU grep's new features about a week ago. (No, the StumbleUpon Firefox plugin was not used.) I printed it out but only got a chance reading it on Thursday.
It talked about several new features in GNU grep 2.5 that's pretty neat. I've already used two of the features at work yesterday. But before I go on, let me see a show of hands, how many of you are using GNU grep 2.5? 2.4? 2.0? Right, chances are that you don't know or much care which version of grep you are using. I didn't know until I read the article. (I'm using GNU grep 2.5.1, which is a patch level behind the latest release. Must download, ./configure; make; make check; make install! ...Not! By the way, 2.5 was out in 2002—four years ago.)
The features I used are called --only-matching and --color. The --only-matching feature can also be specified using the -o option on the command line and it shows only the part of a matching line that matches PATTERN. The --color feature will show the matching portion of the line in color (by default red).
I find myself using the --color option to experiment with the regular expressions and then using the -o option to get the matching text for further processing:
[weiqi@gao]$ cat foo
This is a test.
[weiqi@gao]$ grep --color test foo
This is a test.
[weiqi@gao]$ grep -o test foo
test
For more complicated regular expressions, the color option gives a better visual feedback.