Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I frequently use grep's context command line options (-A, -B, and -C), but occasionally I want to view the whole file with matching expressions highlighted. To accomplish this currently I simply specify -C 999999999, but this seems silly.

Is there an option within OS X's bundled grep that will allow me to easily take advantage of grep's matching and colorizing features, without hiding any file content?

share|improve this question
    
I say "OS X's bundled grep" explicitly because I know that this is BSD grep, and I would rather not install GNU's whole coreutils, assuming GNU grep has this built in. –  Jason Salaz Aug 3 '12 at 8:14
1  
I don't think the bundled grep is capable of this. OTOH you could alias grep='/usr/bin/grep -C 999999999' in interactive shells and forget about it. –  patrix Aug 3 '12 at 9:21
1  
This is not intended to be a permanent change :). (Translation: alias grepall='/usr/bin/grep -C 999999999') –  Jason Salaz Aug 3 '12 at 9:29

3 Answers 3

up vote 6 down vote accepted

I don't think there is any simple option in grep but this seems to work fine. From http://stackoverflow.com/a/981831

grep --color -E "pattern|$" file

The |$ at the end of the regular expression makes grep find pattern OR end of lines. It can't highlight those though, so just the pattern gets colored.

share|improve this answer

Assuming you want to read the file yourself, with the bits highlighted, less is the better tool for the job. less yourfile, type /, your pattern and hit return. n goes to the next match and space to the next page.

Alternatively, ack has a --passthru mode that does what you want. ack is so insanely useful it's worth looking into.

share|improve this answer
    
I love this answer, and may switch to ack eventually. But in the interim, grep is so insanely ingrained in my brain that it's a hard switch to make. But thank you for this wonderful alternative. –  Jason Salaz Dec 19 '12 at 10:08

Setting -C to a negative integer seems to be equivalent to setting it an arbitrarily large number.

grep something --color -C -1 *.txt

But as with something like -C 9999999, if the file has multiple matches, some lines can be printed multiple times.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.