View text in terminal with specific patterns highlighted
I want to view text (could be a file or a piped command output) in the terminal, but I would like to highlight a specific pattern in the text with colors, similarly to how grep
highlights the pattern match in its output lines.
The reason why I can't use standard grep "pattern" /path/to/file
here is because I want to view the entire text (ideally scrollable as if piped through less
) and not just the lines containing the pattern.
Actually, this can be done very easily with some grep
parameters.
The magic command you are looking for is:
grep --color -E "test|$" yourfile
Here's some sample output:
Full disclosure: That command was taken from this answer.
If you're not stuck on color highlighting, you could use less
itself e.g.
less +g -p PATTERN file
or
less +g +/PATTERN file
The +/PATTERN
highlights all instances of PATTERN
in file
, and the +g
suppresses the default behavior of scrolling to the first match.