grep command is the de facto tool for searching text files. However when there are too many matches, it can be difficult to find the requested text in the search results. grep comes with –color=’auto’ option. It surrounds the matching string with the colour, thus resulting enhanced output.
Finding string with color highlighting
Pass –color option to grep command:
# grep --color='auto' -i error /var/log/messages
Output:
............ ... Oct 9 16:12:14 vivek-desktop kernel: [ 11.555442] bt878: probe of 0000:05:00.1 failed with error -22 Oct 10 17:35:28 vivek-desktop kernel: [ 10.564710] bt878: probe of 0000:05:00.1 failed with error -22 Oct 11 10:15:34 vivek-desktop kernel: [ 12.187477] bt878: probe of 0000:05:00.1 failed with error -22 Oct 11 14:29:56 vivek-desktop kernel: [ 11.135309] bt878: probe of 0000:05:00.1 failed with error -22 .......... ... ....
Now all matched text displayed using red color. The –color option to matches in the input in red color by default. Color is added via ANSI escape sequences. To change color use environment variable GREP_COLOR. Following will set background to red and foreground to white:
$ export GREP_COLOR='1;37;41'
$ egrep --color=auto -i '(error|fatal|warn|drop)' /var/log/messages
Output:
I recommend putting following in ~/.bash_profile OR ~/.bashrc file:
$ vi ~/.bash_profile
Append following alias:
export GREP_COLOR='1;37;41'
alias grep='grep --color=auto'
Save and close the file. Please note that –color option works with many GNU text utilities, so feel free to use the same.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 6 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Ven
try to pipe more and less BEFORE grep command like this:
ls /home | more | grep keyword
it works with me.
Thanks, very usefull.
Unfortunately when I pipe the output through ‘more’ it strips out the color. Not even using ‘more -R’ or ‘more -r’ will work. Color output from ‘ls’ does not get stripped, but color output from grep does get stripped out when piped through ‘more’ (or ‘less’). OS: MAC-OSX 10.6.
Ven
try to pipe more and less BEFORE grep command like this:
ls /home | more | grep keyword
it works with me.
Actually, instead of that alias, you should simple place this line in your ~/.bash_extra file:
export GREP_OPTIONS='--color=auto'
Nice tip, thanks a lot!