How to highlight a word without excluding text

Asked by komputes

grep has this cool feature that highlights a work that you are looking for. grep also excludes text other than the pattern you are looking for.

I would like to highlight a word without excluding text and was wondering if this is possible.

The command:
sudo apt-get -y update; sudo apt-get -y upgrade; sudo apt-get -y dist-upgrade

What I would imagine as an alternative (note: just an example, this command does not exist/work):
sudo apt-get -y update; sudo apt-get -y upgrade; sudo apt-get -y dist-upgrade | grep --dont-exclude --color-text Removing

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu grep Edit question
Assignee:
No assignee Edit question
Solved by:
Ralph Corderoy
Solved:
Last query:
Last reply:
Revision history for this message
Best Ralph Corderoy (ralph-inputplus) said :
#1

I think you're asking for all lines in the input to be displayed, even those that don't match. grep(1) has no such option. However, it can display lines of context before and after the matching lines, see the -C, -A, and -B options. So one suggestion is

    grep --color -C 100 "^$USER:" /etc/passwd

The problem with this is that the number of lines of context you specify may not be enough to show all lines.

Alternatively, switch to egrep(1), AKA grep with the -E option, and augment your pattern with one that matches every line but matches no characters in it, then nothing will be highlighted on those lines. The pattern `^' that matches the start of the line is suitable.

    egrep --color "^|^$USER:" /etc/passwd

Revision history for this message
komputes (komputes) said :
#2

Thanks Ralph Corderoy, that solved my question.

Revision history for this message
komputes (komputes) said :
#3

Thats a neat trick Ralph. I setup the following aliases in ~/.bashrc :

alias highlight='egrep --color "^|Removing|Building|Reading|Fetched"'
alias update='sudo apt-get -y update|highlight; sudo apt-get -y upgrade|highlight; sudo apt-get -y dist-upgrade|highlight'

Thanks for your help.