660

I find grep's --color=always flag to be tremendously useful. However, grep only prints lines with matches (unless you ask for context lines). Given that each line it prints has a match, the highlighting doesn't add as much capability as it could.

I'd really like to cat a file and see the entire file with the pattern matches highlighted.

Is there some way I can tell grep to print every line being read regardless of whether there's a match? I know I could write a script to run grep on every line of a file, but I was curious whether this was possible with standard grep.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
zslayton
  • 51,416
  • 9
  • 35
  • 50
  • 5
    if you want more than one color for more than one pattern (i.e. error, warning, info, etc messages), [use `sed`](https://stackoverflow.com/q/14691680/52074). the `sed` solution gets you multiple colors at the cost of added complexity (instead of about 30 characters you have about 60 characters). – Trevor Boyd Smith Sep 14 '18 at 18:39
  • With **sed** you can even **highlight + return exit code**, see example: https://askubuntu.com/a/1200851/670392 – Noam Manos Jan 05 '20 at 16:08
  • @TrevorBoydSmith: With `sed` you could event send a **beep** on console terminal: https://stackoverflow.com/a/69266748/1765658 – F. Hauri - Give Up GitHub Sep 21 '21 at 09:54

24 Answers24

1026

Here are some ways to do it:

grep --color 'pattern\|$' file
grep --color -E 'pattern|$' file
egrep --color 'pattern|$' file

The | symbol is the OR operator. Either escape it using \ or tell grep that the search text has to be interpreted as regular expressions by adding -E or using the egrep command instead of grep.

The search text "pattern|$" is actually a trick, it will match lines that have pattern OR lines that have an end. Because all lines have an end, all lines are matched, but the end of a line isn't actually any characters, so it won't be colored.

To also pass the colored parts through pipes, e.g. towards less, provide the always parameter to --color:

grep --color=always 'pattern\|$' file | less -r
grep --color=always -E 'pattern|$' file | less -r
egrep --color=always 'pattern|$' file | less -r
Abdull
  • 26,371
  • 26
  • 130
  • 172
Ryan Oberoi
  • 13,817
  • 2
  • 24
  • 23
  • 180
    That |$ trick is neat! Well done, I'll have to remember that. For those of you that aren't regular expression savvy, "pattern|$" will match lines that have the pattern you're searching for AND lines that have an end -- that is, all of them. Because the end of a line isn't actually any characters, the colorized portion of the output will just be your pattern. Thanks Ryan! – zslayton Jun 11 '09 at 15:36
  • 67
    You can also omit the "$": `egrep --color "pattern|" file` (credit http://stackoverflow.com/a/7398092/50979) – 13ren Dec 08 '12 at 11:50
  • 17
    @Zack , the "|" operator is an OR operator, not an AND, – JBoy Feb 18 '13 at 12:20
  • 18
    @JBoy, I was using 'AND' in the conventional English way rather than the boolean logic way. You're correct, it is indeed an 'or' operator -- it matches this and that. :P Good clarification. – zslayton Feb 19 '13 at 15:32
  • 13
    It appears that the "$" is needed if matching more than a one pattern. `egrep --color "pattern1|pattern2|$"`. Otherwise the color highlighting does not happen. – ZaSter Sep 19 '13 at 00:10
  • 6
    You don't have to use the -E flag (extended regular expression) if you escape the '|', like so: `grep --color "pattern\|$" file` – qwertyboy Feb 21 '14 at 12:40
  • 2
    "$" is required for a [regex-directed engine](http://www.regular-expressions.info/engine.html), like the `grep` on OS X Mountain Lion and Mavericks. – willkil Apr 01 '14 at 19:48
  • 1
    You don't even have to use the extended regex flag (`-e`), basic regex, which is the default, can match multiple patterns if the pipe is escaped. So this works for me: `grep 'pattern\|'`. – qwertyboy Nov 22 '14 at 19:00
  • 4
    If you are going to pipe this into less and still want the output to be colored then you probably want to use grep --color=always and alias less='less -r' – wytten Feb 19 '15 at 15:06
  • 3
    @qwertyboy That is a feature of *GNU* `grep`. POSIX-compliant `grep` (*not*: `egrep`) supports only [*Basic* Regular Expressions (BREs)](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_03) without `-E`, where alternation is not possible. (You should always use `egrep` instead of `grep -E`, to be sure you can use [*Extended* Regular Expressions (EREs)](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04). `grep` implementations vary.) – PointedEars Jun 23 '15 at 17:32
  • 1
    @qwertyboy `-e` is different from `-E`. `-e` marks the following positional argument as a regular expression (so that it can begin with, e.g., `-`, and is still not considered an option); `-E` *additionally* enables ERE support. See the [POSIX `grep` specification](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html). – PointedEars Jun 23 '15 at 17:42
  • Can we use different color for different matches -- egrep --color "pattern1|pattern2|$ – Pradeep Feb 10 '16 at 20:25
  • Does this work well with "live" output? I'd like to colorize live logs from my test application by piping to a grep instance. – Atilla Filiz Apr 22 '16 at 07:27
  • 1
    `grep -E` is preferred over `egrep` since many years back. The answer seems to imply the opposite. – tripleee Jan 26 '17 at 13:25
  • I would still use `--color=always` and not only `--color`, as the latter won't preserve the colorization when you pipe grep to another command, for example to `less -R`. – HelloGoodbye May 22 '17 at 13:43
  • @13ren there are some variants of grep that optimize away "|", so 'pattern|$' is better (see https://stackoverflow.com/a/13979036/939457). – Sorin Mar 04 '19 at 12:34
  • Nice trick! Will this work when combined with other options, such as -F and -w? In my case no matches appear. I also prefer to have the non-matching lines highlighted as context. But, -C999 has problems too. – Dr. Alex RE Apr 18 '20 at 20:08
  • A question on a related topic: how do I highlight the text not matching the pattern using `grep`? – Harish May 18 '20 at 01:09
  • the `-E` is a short for **`--extended-regexp `** if you, like me, like readability more than letter magic. – jave.web Mar 02 '21 at 13:58
126

Here's something along the same lines. Chances are, you'll be using less anyway, so try this:

less -p pattern file

It will highlight the pattern and jump to the first occurrence of it in the file.

You can jump to the next occurence with n and to the previous occurence with p. Quit with q.

mit
  • 11,083
  • 11
  • 50
  • 74
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
52

I'd like to recommend ack -- better than grep, a power search tool for programmers.

$ ack --color --passthru --pager="${PAGER:-less -R}" pattern files
$ ack --color --passthru pattern files | less -R
$ export ACK_PAGER_COLOR="${PAGER:-less -R}"
$ ack --passthru pattern files

I love it because it defaults to recursive searching of directories (and does so much smarter than grep -r), supports full Perl regular expressions (rather than the POSIXish regex(3)), and has a much nicer context display when searching many files.

ephemient
  • 198,619
  • 38
  • 280
  • 391
  • 2
    However, from time to time, it does not find what I want when I’m certain it must be there. `ack` is smart, but sometimes too smart, and it exluded the file type that the hit was in. – Michael Piefel Jul 24 '12 at 19:50
  • 4
    @MPi `ack -a` will search all file types, while still excluding `.git/` `.svn/` etc. – ephemient Jul 24 '12 at 20:36
  • 1
    However, it is cool that `ack` does not search through my images, so `-a` does too much. I added `--type-set=freemarker=.ftl` to my `~/.ackrc`, to give one example. – Michael Piefel Jul 25 '12 at 11:51
  • 4
    With a few config tweaks, grep already does everything ack does, is faster, and never omits results like ack's whitelists sometimes do. Perhaps save your preferred grep settings in .bashrc. Mine reads: function grp() { GREP_OPTIONS="-rI --color --exclude-dir=\.git --exclude=tags" grep "$@" – Jonathan Hartley Jul 20 '13 at 15:50
32

You can use my highlight script from https://github.com/kepkin/dev-shell-essentials

It's better than grep because you can highlight each match with its own color.

$ command_here | highlight green "input" | highlight red "output"

Screen shot from Github project

tripleee
  • 175,061
  • 34
  • 275
  • 318
kepkin
  • 1,105
  • 11
  • 14
  • 5
    The question expressly asked for a solution using `grep`, which is a standard utility on machines running *nix. – zslayton Aug 18 '14 at 19:48
  • 2
    This script is good, but not as good as `colout` mentioned in another answer. – Jonathan Hartley Dec 15 '15 at 21:24
  • @JonathanHartley And why is that so? I see no reason for it. Besides, this script uses a much simpler implementation than `colout`, which is good if you want to inspect what it does. – HelloGoodbye Jan 10 '19 at 17:03
  • @HelloGoodbye Yeah, fair enough. I should hold off on the judgement. colout is more thorough and powerful, but you are right that it is correspondingly more complex to use and reverse-engineer. – Jonathan Hartley Jan 15 '19 at 00:13
  • @JonathanHartley It makes sense that it is more powerful! – HelloGoodbye Jan 15 '19 at 16:07
  • @HelloGoodbye, When choosing between a script utility and a hard-coded utility, I _always_ opt for the hard coded utility. Just because you can see "what a script is doing" doesn't mean that something as fundamental as character escaping won't derail some small part of it. I've bumped into all manner of trouble with various 3rd party bash/zsh that become laden with assumptions about just how POSIX they really think they are on this or that {uni,linu}x distro, and that is especially true in the era of mixed Unicode vs ASCII files. – alife Mar 11 '22 at 14:29
23

You can also create an alias. Add this function in your .bashrc (or .bash_profile on osx)

function grepe {
    grep --color -E "$1|$" $2
}

You can now use the alias like this: "ifconfig | grepe inet" or "grepe css index.html".

(PS: don't forget to source ~/.bashrc to reload bashrc on current session)

Fabien Sa
  • 9,135
  • 4
  • 37
  • 44
21

The -z option for grep is also pretty slick!

cat file1 | grep -z "pattern"
Abhishek Jaisingh
  • 1,614
  • 1
  • 13
  • 23
  • what does this doe? -z tells grep to use ASCII NUL as a line delimiter... – vy32 Oct 31 '19 at 15:56
  • @vy32 -z essentially translates the entire file to a single line. However, the replier presumed that grep also has the "--color" option set by default, which many people have configured in their alias for grep, but is not a default. – mmigdol Jul 16 '20 at 22:16
  • 2
    This was the answer I was looking for. Works well when used in a pipe. – rickgn Mar 15 '21 at 10:52
  • 1
    **_WARNING:_** @rickgn, This does _not_ pass through anything if none of the input lines have any match. Test with `echo "hello\nthere" | grep -z x`. – alife Oct 16 '21 at 18:02
  • @alife good point, and it's actually kind of cool, because what's the point of seeing any text if the pattern is not in the text? if long text, seeing no output could be faster that scanning the output just to find out there are no matches. :) – Oliver Mar 09 '22 at 23:03
  • @Oliver, I would argue that to be a terrible violation of the "Principal of Least Astonishment" (AKA, "The Law of Least Surprise"). The question was how to cat out the file showing the matches as highlighted. Not dump all the output if there are no matches. For instance, with that technique it would be impossible to reliably daisy chain multiple greps for multiple potential matches. So, no, I suspect most would not find that kind of anti-highlighter "actually kind of cool." – alife Mar 11 '22 at 13:16
  • @alife good point about the chaining, but grep does not show any results when no pattern match so it is not least surprise. In fact it is the opposite, if there was output without pattern match, user would be confused. – Oliver Mar 11 '22 at 14:47
  • (?) @Oliver, we are _creating functionality_. To show me the file (not show me nothing) with specified items highlighted. That it's using `grep` underneath (or awk, sed, perl, C, etc.) does not impact any Law of Least Surprise. The goal as stated is _"viewing the entire file with highlighted matches"_. No one would want to file to vanish suddenly given that criteria. Perhaps if it were called _"viewing the entire file with highlighted matches but yielding nothing if there are no matches"_. Anyway, be well; I'm tiring of this silly argument. – alife Mar 27 '22 at 12:44
20

Use colout program: http://nojhan.github.io/colout/

It is designed to add color highlights to a text stream. Given a regex and a color (e.g. "red"), it reproduces a text stream with matches highlighted. e.g:

# cat logfile but highlight instances of 'ERROR' in red
colout ERROR red <logfile

You can chain multiple invocations to add multiple different color highlights:

tail -f /var/log/nginx/access.log | \
    colout ' 5\d\d ' red | \
    colout ' 4\d\d ' yellow | \
    colout ' 3\d\d ' cyan | \
    colout ' 2\d\d ' green

Or you can achieve the same thing by using a regex with N groups (parenthesised parts of the regex), followed by a comma separated list of N colors.

vagrant status | \
    colout \
        '\''(^.+  running)|(^.+suspended)|(^.+not running)'\'' \
        green,yellow,red
Jonathan Hartley
  • 15,462
  • 9
  • 79
  • 80
user2683246
  • 3,399
  • 29
  • 31
  • 1
    As noted elsewhere, the question expressly asked for a solution using grep, which is a standard utility on machines running *nix. – zslayton Jun 19 '15 at 13:18
  • 6
    @Zack ok, sorry. Actually, if you expand the problem beyond `grep`, and it is already expanded in the answers, `colout` is the best solution for the problem you had, the best that I'm aware of. According to UNIX philosophy, programs should be written to do one thing well. For `grep` it is filtering text stream. For `colout` it is colorizing or highlighting text stream. – user2683246 Jun 19 '15 at 23:08
  • This is the best answer, because it can apply multiple different colored highlights, and `colout` is such a widely-useful tool. Learn it once, use it in many situations, rather than learning one tool to highlight logfiles, another to highlight test output, etc. – Jonathan Hartley Dec 15 '15 at 21:20
13

As grep -E '|pattern' has already been suggested, just wanted to clarify that it's possible to highlight a whole line too.

For example, tail -f somelog | grep --color -E '| \[2\].*' (specifically, the part -E '|):

Artfaith
  • 1,183
  • 4
  • 19
  • 29
9

I use rcg from "Linux Server Hacks", O'Reilly. It's perfect for what you want and can highlight multiple expressions each with different colours.

#!/usr/bin/perl -w
#
#       regexp coloured glasses - from Linux Server Hacks from O'Reilly
#
#       eg .rcg "fatal" "BOLD . YELLOW . ON_WHITE"  /var/adm/messages
#
use strict;
use Term::ANSIColor qw(:constants);

my %target = ( );

while (my $arg = shift) {
        my $clr = shift;

        if (($arg =~ /^-/) | !$clr) {
                print "Usage: rcg [regex] [color] [regex] [color] ...\n";
                exit(2);
        }

        #
        # Ugly, lazy, pathetic hack here. [Unquote]
        #
        $target{$arg} = eval($clr);

}

my $rst = RESET;

while(<>) {
        foreach my $x (keys(%target)) {
                s/($x)/$target{$x}$1$rst/g;
        }
        print
}
dave1010
  • 15,135
  • 7
  • 67
  • 64
9

I added this to my .bash_aliases:

highlight() {
  grep --color -E "$1|\$"
}
whoan
  • 8,143
  • 4
  • 39
  • 48
uronce
  • 145
  • 1
  • 5
5

Use ripgrep, aka rg: https://github.com/BurntSushi/ripgrep

rg --passthru...

Color is the default:

enter image description here

  rg -t tf -e  'key.*tfstate' -e dynamodb_table
       --passthru
       Print both matching and non-matching lines.

       Another way to achieve a similar effect is by modifying your pattern to
       match the empty string. 
       For example, if you are searching using rg foo then using 
       rg "^|foo" instead will emit every line in every file searched, but only
       occurrences of foo will be highlighted. 
       This flag enables the same behavior without needing to modify the pattern.

Sacrilege, granted, but grep has gotten complacent.

brew/apt/rpm/whatever install ripgrep

You'll never go back.

Bruce Edge
  • 1,975
  • 1
  • 23
  • 31
  • Thanks, this helped. Searching in dependency tree is now easier. `rg --passthru 'setuptools' <(poetry show --tree)` – Adam Jun 30 '22 at 13:34
  • 1
    Since we're allowing to go off-topic by suggesting different options to `grep`, may I introduce to you [`ugrep`](https://github.com/Genivia/ugrep#performance-comparisons), which beats `ripgrep` in performance — and basically everything else out there — while, at the same time, adding lots of extra goodies. In my case, since `ugrep` includes the `--replace` option, I've since given up on `sed` and its quirky handling of regexps... and, unlike standard `grep`, `ugrep` is _much_ faster than `sed`. – Gwyneth Llewelyn Jul 18 '22 at 09:11
5

The way

As there is already a lot of different solution, but none show sed as solution,
and because sed is lighter and quicker than grep, I prefer to use sed for this kind of job:

sed 's/pattern/\o33[47;31;1m&\o033[0m/' file

This seems less intuitive.

  • s/pattern/replaced/ is sed replacement command to replace pattern by replaced.
  • \o33 is the sed syntax to generate the character octal 033 -> Escape.
    (Some shells and editors also allow entering <Ctrl>-<V> followed by <Esc>, to type the character directly.)
  • Esc [ 47 ; 31 ; 1 m is an ANSI escape code: Background grey, foreground red and bold face.
  • & will re-print the pattern.
  • Esc [ 0 m returns the colors to default.

You could also highlight the entire line, but mark the pattern as red:

sed -E <file -e \
    's/^(.*)(pattern)(.*)/\o33[30;47m\1\o33[31;1m\2\o33[0;30;47m\3\o33[0m/'

Dynamic tail -f, following logfiles

One of advantage of using sed: You could send a alarm beep on console, using bell ascii character 0x7. I often use sed like:

sudo tail -f /var/log/kern.log |
    sed -ue 's/[lL]ink .*\([uU]p\|[dD]own\).*/\o33[47;31;1m&\o33[0m\o7/'
  • -u stand for unbuffered. This ensure that line will be treated immediately.

So I will hear some beep instantly, when I connect or disconnect my ethernet cable.

Of course, instead of link up pattern, you could watch for USB in same file, or even search for from=.*alice@bobserver.org in /var/log/mail.log (If you're Charlie, anxiously awaiting an email from Alice;)...

Advantage of sed

As sed is a language, you could use many directives, for sample: avoiding imap and pop logs while watching for incoming request on some mail server:

tail -f /var/log/mail.log | sed -ue '
    /[[:space:]]\(imap\|pop\)d\[/d;
    /[^[:alnum:]]smtpd\[/{ 
        s/.*/\o33[30;47m&\o33[0m/;
        s/\(alice\|bob\)@example.com/\o33[31;1m\o7&\o33[30m/;
    }
'

This will

  • delete lines regarding imapd or popd servers.
  • highlight lines regarding smtpd server and
  • in smtpd lines highlight and beep when line containing alice@example.com or bob@example.com is found.
F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137
3

To highlight patterns while viewing the whole file, h can do this.

Plus it uses different colors for different patterns.

cat FILE | h 'PAT1' 'PAT2' ...

You can also pipe the output of h to less -R for better reading.

To grep and use 1 color for each pattern, cxpgrep could be a good fit.

treulz
  • 51
  • 1
2

another dirty way:

grep -A80 -B80 --color FIND_THIS IN_FILE

I did an

alias grepa='grep -A80 -B80 --color'

in bashrc.

Kobunite
  • 365
  • 8
  • 23
fly_a320
  • 21
  • 1
  • 1
    this is problematic if the things you're looking for isn't there. Say due to an error, in which case you'll get nothing. – Paul Rubel Sep 12 '13 at 18:25
1

One other answer mentioned grep's -Cn switch which includes n lines of Context. I sometimes do this with n=99 as a quick-and-dirty way of getting [at least] a screenfull of context when the egrep pattern seems too fiddly, or when I'm on a machine on which I've not installed rcg and/or ccze.

I recently discovered ccze which is a more powerful colorizer. My only complaint is that it is screen-oriented (like less, which I never use for that reason) unless you specify the -A switch for "raw ANSI" output.

+1 for the rcg mention above. It is still my favorite since it is so simple to customize in an alias. Something like this is usually in my ~/.bashrc:

alias tailc='tail -f /my/app/log/file | rcg send "BOLD GREEN" receive "CYAN" error "RED"'

MarkHu
  • 1,694
  • 16
  • 29
1

Alternatively you can use The Silver Searcher and do

ag <search> --passthrough
Andrew Magee
  • 6,506
  • 4
  • 35
  • 58
1

I use following command for similar purpose:

grep -C 100 searchtext file

This will say grep to print 100 * 2 lines of context, before & after of the highlighted search text.

Al Mamun
  • 944
  • 9
  • 27
1

It might seem like a dirty hack.

grep "^\|highlight1\|highlight2\|highlight3" filename

Which means - match the beginning of the line(^) or highlight1 or highlight2 or highlight3. As a result, you will get highlighted all highlight* pattern matches, even in the same line.

Serhii Nadolynskyi
  • 5,473
  • 3
  • 21
  • 20
1

Here is a shell script that uses Awk's gsub function to replace the text you're searching for with the proper escape sequence to display it in bright red:

#! /bin/bash
awk -vstr=$1 'BEGIN{repltext=sprintf("%c[1;31;40m&%c[0m", 0x1B,0x1B);}{gsub(str,repltext); print}' $2

Use it like so:

$ ./cgrep pattern [file]

Unfortunately, it doesn't have all the functionality of grep.

For more information , you can refer to an article "So You Like Color" in Linux Journal

Wernsey
  • 5,411
  • 22
  • 38
0

If you want highlight several patterns with different colors see this bash script.

Basic usage:

echo warn error debug info 10 nil | colog

You can change patterns and colors while running pressing one key and then enter key.

Edoot
  • 391
  • 1
  • 4
0

Here's my approach, inspired by @kepkin's solution:

# Adds ANSI colors to matched terms, similar to grep --color but without
# filtering unmatched lines. Example:
#   noisy_command | highlight ERROR INFO
#
# Each argument is passed into sed as a matching pattern and matches are
# colored. Multiple arguments will use separate colors.
#
# Inspired by https://stackoverflow.com/a/25357856
highlight() {
  # color cycles from 0-5, (shifted 31-36), i.e. r,g,y,b,m,c
  local color=0 patterns=()
  for term in "$@"; do
    patterns+=("$(printf 's|%s|\e[%sm\\0\e[0m|g' "${term//|/\\|}" "$(( color+31 ))")")
    color=$(( (color+1) % 6 ))
  done
  sed -f <(printf '%s\n' "${patterns[@]}")
}

This accepts multiple arguments (but doesn't let you customize the colors). Example:

$ noisy_command | highlight ERROR WARN
dimo414
  • 47,227
  • 18
  • 148
  • 244
0

Is there some way I can tell grep to print every line being read regardless of whether there's a match?

Option -C999 will do the trick in the absence of an option to display all context lines. Most other grep variants support this too. However: 1) no output is produced when no match is found and 2) this option has a negative impact on grep's efficiency: when the -C value is large this many lines may have to be temporarily stored in memory for grep to determine which lines of context to display when a match occurs. Note that grep implementations do not load input files but rather reads a few lines or use a sliding window over the input. The "before part" of the context has to be kept in a window (memory) to output the "before" context lines later when a match is found.

A pattern such as ^|PATTERN or PATTERN|$ or any empty-matching sub-pattern for that matter such as [^ -~]?|PATTERN is a nice trick. However, 1) these patterns don't show non-matching lines highlighted as context and 2) this can't be used in combination with some other grep options, such as -F and -w for example.

So none of these approaches are satisfying to me. I'm using ugrep, and enhanced grep with option -y to efficiently display all non-matching output as color-highlighted context lines. Other grep-like tools such as ag and ripgrep also offer a pass-through option. But ugrep is compatible with GNU/BSD grep and offers a superset of grep options like -y and -Q. For example, here is what option -y shows when combined with -Q (interactive query UI to enter patterns):

ugrep -Q -y FILE ...
Dr. Alex RE
  • 1,772
  • 1
  • 15
  • 23
  • Why vote this down without leaving a comment? It's more than fair to mention alternative grep tools, same as some of the other answers. – Dr. Alex RE May 29 '20 at 17:33
0

Also try:

egrep 'pattern1|pattern2' FILE.txt | less -Sp 'pattern1|pattern2'

This will give you a tabular output with highlighted pattern/s.

MAPK
  • 5,635
  • 4
  • 37
  • 88
0

Ok, this is one way,

wc -l filename

will give you the line count -- say NN, then you can do

grep -C NN --color=always filename
nik
  • 13,254
  • 3
  • 41
  • 57
  • 3
    "-C 2147483647" if you don't want to wc first. Using a large number here doesn't appear to slow things down. –  Apr 21 '15 at 18:11
  • Where is the search string, and will this output anything if there are no matches as a highlighter is expected to? For example, `echo "hello\nthere" | grep -C99 x` produces nothing. – alife Oct 16 '21 at 18:10