165

Say I have the following file:

$ cat test

test line 1
test line 2
line without the search word
another line without it
test line 3 with two test words
test line 4

By default, grep returns each line that contains the search term:

$ grep test test

test line 1
test line 2
test line 3 with two test words
test line 4

Passing the --color parameter to grep will make it highlight the portion of the line that matches the search expression, but it still only returns lines that contain the expression. Is there a way to get grep to output every line in the source file, but highlight the matches?

My current terrible hack to accomplish this (at least on files that don't have 10000+ consecutive lines with no matches) is:

$ grep -B 9999 -A 9999 test test

Screenshot of the two commands

If grep can't accomplish this, is there another command-line tool that offers the same functionality? I've fiddled with ack, but it doesn't seem to have an option for it either.

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
  • 1
    Possible cross site duplicate of: http://stackoverflow.com/questions/981601/colorized-grep-viewing-the-entire-file-with-highlighting The top answer is the same on both. – Ciro Santilli OurBigBook.com Feb 16 '14 at 14:29
  • 3
    You can use -C 9999 in place of -A 9999 -B 9999.I always do: grep -C 9999 pattern file – neo Aug 28 '16 at 12:38
  • Try sed. For example, here's how to highlight + return exit code: https://askubuntu.com/a/1200851/670392 – Noam Manos Jan 05 '20 at 16:10
  • -C9999 (or -C99 for a short input) might actually be high up on the list of elegance/availability, and gives the right return value if it finds a match. The only downside is it won't print anything if it doesn't find one. – mwfearnley May 18 '22 at 09:09

13 Answers13

233
grep --color -E "test|$" yourfile

What we're doing here is matching against the $ pattern and the test pattern, obviously $ doesn't have anything to colourize so only the test pattern gets color. The -E just turns on extended regex matching.

You can create a function out of it easily like this:

highlight () { grep --color -E "$1|$" "${@:1}" ; }
jacksonh
  • 3,734
  • 1
  • 20
  • 13
  • 15
    That's freaking awesome! – gvkv Aug 12 '10 at 03:09
  • 3
    And as a function: highlight () { grep --color -E "$1|$" $2 ; }. Usage: highlight test yourfile – Stefan Lasiewski Oct 14 '10 at 03:48
  • 2
    @StefanLasiewski: "$2" should also be quoted. – Dennis Williamson Mar 17 '11 at 20:04
  • 6
    Better could be: highlight () { grep --color -E "$1|$" "$@" } which allows files with whitespace in their names and multiple files. – Mike DeSimone Jul 24 '11 at 05:34
  • 15
    @MikeDeSimone - But that will also have "$1" in the files. Use highlight () { grep --color -E "$1|$" "${@:1}" } – Chris Down Nov 17 '11 at 11:48
  • I have my LESS environment set to (amongst other flags) -r, so I've modified the alias to grep --color=always ... so long files can be piped to less and still have the highlight :) – Drav Sloan Aug 29 '13 at 02:28
  • What do you mean when you say you're matching against the "$ pattern"? Doesn't the $ character mean end of line? – Nate Nov 22 '14 at 21:40
  • 3
    I came up with this ${@:2} with bash because you dont want the first argument to be repeated in the list files to check. highlight() { grep --color -E -- "$1|\$" "${@:2}"; }. Note the -- also to signigy that arguments with dash are done, so if you search a pattern line -something its still working. – Lynch Mar 13 '16 at 05:08
  • I find the same result as @Lynch. Does the answer as written work for anyone? – Jack O'Connor Aug 28 '17 at 19:42
  • 1
    you don't need the dollar after the OR - grep --color -E "test|" yourfile ... that might help cuz now you don't need to escape in the double-quote string – nhed Jan 02 '20 at 02:36
  • You can also use the shorter version highlight () { grep --color -E "$1|$" ; } if you only want to pipe into it, like helm deploy | highlight configured for example. – Tim May 09 '20 at 21:27
  • @Lynch: reverse the order of arguments to grep to not need the -- : highlight() { grep --color -E "^|$1" "${@:2}"; } – Olivier Dulac Dec 19 '20 at 00:18
  • 1
    @OlivierDulac the -- are not something you would want to remove. It is something to make your script more robust. You don't have to fight the double dash. What if "${@:2}" == --something-funny? – Lynch Dec 21 '20 at 03:15
  • Totally power hack – Dean P Feb 05 '22 at 13:04
46
ack --passthru --color string file

for Ubuntu and Debian, use ack-grep instead of ack

ack-grep --passthru --color string file
18

Another way to do this properly and portably with grep (besides using two regexes with alternation as in the accepted answer) is via the null pattern (and respectively null string).
It should work equally well with both -E and -F switches since, per the standard:

-E
    Match using extended regular expressions. 
    [...] A null ERE shall match every line.

and

-F
    Match using fixed strings.
    [...] A null string shall match every line.

So it's simply a matter of running

grep -E -e '' -e 'pattern' infile

and respectively

grep -F -e '' -e 'string' infile
don_crissti
  • 82,805
12

I have the following function that I use for such things:

highlight () {
    perl -pe "s/$1/\e[1;31;43m$&\e[0m/g"
}

Internally it looks kind of ugly, it's nice and easy to use, like so:

cat some_file.txt | highlight some_word

or, for a slightly more real-world example:

tail -f console.log | highlight ERROR

You can change the colors to anything you like (which might be hard with grep--I'm not sure) by changing the 1 and 31 and 43 (after \e[) to different values. The codes to use are all over the place, but here's a quick intro: the 1 bolds the text, the 31 makes it red, and the 43 gives a yellow background. 32 or 33 would be different colors, and 44 or 45 would be different backgrounds: you get the idea. You can even make it blink (with a 5) if you're so inclined.

This doesn't use any special Perl modules, and Perl is nearly ubiquitous, so I would expect it to work just about anywhere. The grep solution is very clever, but the --color switch on grep is not available everywhere. For instance, I just tried this solution on a Solaris box running bash, and another running ksh, and my local Mac OS X machine running zsh. All worked just fine. Solaris choked on the grep --color solution, however.

Also, ack is awesome, and I recommend it to anyone who hasn't yet discovered it, but I've had some issues installing it on a few of the many servers I work on. (I forget why: I think related to Perl modules it required.)

Since I don't think I've ever worked on a Unix box that didn't have Perl installed (with the exception of embedded-type systems, Linksys routers, and such) I'd say this is pretty much a universally useable solution.

iconoclast
  • 9,198
  • 13
  • 57
  • 97
  • I like this solution because it does just what you want rather than bend and existing tool like grep which is more indirect. It also gives you more flexibility should you want to change the behavior. (Although just a note you can change the colors with grep with the env var GREP_COLORS which uses the same SGR codes). – wardw Dec 17 '20 at 16:16
  • Also just a note that the colors are reset with \e[0m after the match and so this will also clear any existing colorization in the input stream. That's also true of grep, however. If you need to handle input that's already colorized, you might consider wrapping your match text with the terminal controls tput smso and tput rmso instead. – wardw Dec 17 '20 at 16:20
7

ripgrep

Use ripgrep with its --passthru parameter:

rg --passthru pattern file.txt

It's one of the fastest grepping tools, since it's built on top of Rust's regex engine which uses finite automata, SIMD and aggressive literal optimizations to make searching very fast.

--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.

kenorb
  • 20,988
  • 1
    This is the most helpful answer, especially since the exit code isn't zero when the pattern wasn't matched.

    For example: grep --color -E "test|$" yourfile just highlights all occurrences of "test" in the file and even when there are no matches, it still returns 0 which is counter-intuitive.

    However: rg --passthru test yourfile highlights the occurrences but returns a non-zero exit code when the pattern wasn't matched.

    – Miraclx Feb 11 '22 at 22:26
3

Instead of using Grep, you can use Less:

less file

Search like this: /pattern Enter

This will:

  1. scroll to first matching line
  2. output every line starting from there on
  3. highlight all matches

To scroll to next matching line: n

To scroll to previous matching line: N

To toggle highlighting: Esc u

Also you can change the highlighting color if you like.

Zombo
  • 1
  • 5
  • 44
  • 63
3

If you're interested in the return value of grep, then GNU sed is also the way:

sed '/pattern/h; ${p;x;/./Q0;Q1}'

You can add to the script modifications to the line, e.g.:

sed '/pattern/h; ${p;x;/./Q0;Q1}; s/pattern/<\0>/g'

This checks that pattern appears on the current line, and stores it in the hold buffer if it is. At the end of the file ($), the last line is printed (p), then the hold space and pattern space are swapped (x). Next, the pattern space is tested for emptiness (/./), and if it is nonempty, exit with code 0 (Q0) otherwise with code 1 (Q1).

An alternative is /pattern/{:a $q1; n; ba}, which loops after detecting pattern, but changing the current line can get tricky with this. This works by:

  • /pattern/: the following clause will only be executed when the pattern is detected on a line.
  • :a … ba: this is defining a label a and branching to it; as is, this is an infinite loop.
  • n: prints the current line and loads the next one.
  • $q1: when reaching the end of file ($), exit with code 1.

By default, sed will exit with code 0, so with this expression, if the pattern is detected, the rest of the file is dumped and an exit code of 1 is produced.

Michaël
  • 774
2

There's a much easier way to do this for GNU grep but I don't think it's portable (i.e., BSD grep):

In a pipe:

cat <file> | grep --color=always -z <query>

On a file:

grep --color=always -z <query> <file>

Credit goes to Cyrus's answer here.

  • 2
    This isn’t as good an answer as you (and Cyrus) think.  It has the effect of treating the entire file as a single line.  Therefore, (1) if the file is very large, there may be a possibility of running out of memory, and (2) if the file doesn’t contain the pattern at all, then nothing will be output. And you’re right; it isn’t universally available. (But then again, --color isn’t standard either.) – G-Man Says 'Reinstate Monica' Feb 06 '17 at 04:43
  • What version of grep is this supported on? On grep 2.5.4, -z doesn't seem available... – Alex Jun 29 '17 at 22:29
2

You can try:

perl -MTERM::ANSIColor -nle '/pattern/ ? print colored($_, 'color') : print' test

Not very portable however, and even if Perl is installed, you may need to download another module. In addition it will color the entire line, not just the search word.

gvkv
  • 2,738
2

None of the answers given so far do provide a portable solution.

Here is a portable1 shell function I already posted in a closed as duplicate question that doesn't require non standard tools or non standard extensions provided with perl, ack, ggrep, gsed, bash and the likes but only needs a POSIX shell and the POSIX mandatory utilities sed and printf:

grepc()
{
  pattern=$1
  shift
  esc=$(printf "\033")
  sed 's"'"$pattern"'"'$esc'[32m&'$esc'[0m"g' "$@"
}

You can use it that way:

grepc string_to_search [file ...]

The highlight color can be adjusted by using one of these codes in the sed command argument (32m being green here):

30m black
31m red
33m yellow
34m blue
35m magenta
36m cyan
37m white
7m reverse video

1 As long as your terminal supports ANSI colors escape sequences.

jlliagre
  • 61,204
0

A sed version, works on both bash and ash.

#highlight
hsed(){
    local pattern="$1"
    shift
    local r=`echo -e '\e'[31m`
    local c=`echo -e '\e'[0m`
    sed "s:${pattern//:/\:}:$r\0$c:g" "$@"
}
yurenchen
  • 266
0

OP asked for grep, and that is what I RECOMMEND; but after trying hard to solve a problem with sed, for the record, here is a simple solution with it:

sed $'s/main/\E[31m&\E[0m/g' testt.c

or

cat testt.c | sed $'s/main/\E[31m&\E[0m/g'

Will paint main in red.

  • \E[31m : red color start sequence
  • \E[0m : finished color mark
  • & : the matched pattern
  • /g : all words in a line, not just the first
  • $'string' is bash strings with escaped characters interpreted

Regarding grep, it also works using ^ (begin of line) instead of $ (end of line). Example:

egrep "^|main" testt.c

And just to show this crazy alias that I DO NOT RECOMMEND, you can even let the open quotes:

alias h='egrep -e"^|'
h main" testt.c
cat testt.c | h main"

all work! :) Don't worry if you forget to close the quote, bash will remember you with a "continuing line character".

DrBeco
  • 774
0

Similar to the perl answer, but I use it with multiple expressions separated by |:

tail -F somefile.log | perl -pe 's/\bfirstword\b/\e[1;31m$&\e[0m/g|s/\bsecondword\b/\e[1;32m$&\e[0m/g'

You and add more words by append multiple | (i.e., |s/\bmorewords\b/\e[1;32m$&\e[0m/g) and on What color codes can I use in my bash PS1 prompt? you can see more bash the colors to use.

user
  • 781