others-How to grep any string with highlights or color
1. Purpose
In this post, I would demonstrate how to grep any string with highlights or color, just as follows:
2. The solution
2.1 The solution
Let’s say if you want to grep dynamic
string from output of nginx -V
, you can do as follows:
nginx -V 2>&1|grep --color -- dynamic
Then you can get this:
2.2 The explanation: Why does it work?
Let’s check what does nginx -V
output:
⚡ root@launch-advisor-20191120 ~ nginx -V
nginx version: nginx/1.20.0
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/var/www/html --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-pcre --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_image_filter_module=dynamic --modules-path=/etc/nginx/modules --with-http_v2_module --with-stream=dynamic --with-http_addition_module --with-http_mp4_module
If you grep the string directly ,you would not see any color or highlight of the string:
⚡ root@launch-advisor-20191120 ~ nginx -V|grep --color -- dynamic
nginx version: nginx/1.20.0
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/var/www/html --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-pcre --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_image_filter_module=dynamic --modules-path=/etc/nginx/modules --with-http_v2_module --with-stream=dynamic --with-http_addition_module --with-http_mp4_module
The above command shows that the ‘dynamic’ string is not highlighted or colored in the grep result.
Then we add 2&1
to the nginx -V
command and grep again:
2.3 What is 2>&1
and why does it work?
So the key point is to use 2>&1
on the results of the output , then grep can show color, but what does 2>&1
mean?
You use &1 to reference the value of the file descriptor 1 ( stdout ). So when you use 2>&1 you are basically saying “Redirect the stderr to the same place we are redirecting the stdout ”.
Here is a demo :
⚡ root@launch-advisor-20191120 ~ echo "test"
test
⚡ root@launch-advisor-20191120 ~ echo "test">a.txt
⚡ root@launch-advisor-20191120 ~ cat a.txt
test
⚡ root@launch-advisor-20191120 ~ echo "test" 1> a.txt
⚡ root@launch-advisor-20191120 ~ cat a.txt
test
⚡ root@launch-advisor-20191120 ~ echo "test" 2> a.txt
test
⚡ root@launch-advisor-20191120 ~ cat a.txt
⚡ root@launch-advisor-20191120 ~ echo "test" 1>&2
test
⚡ root@launch-advisor-20191120 ~ echo "test" 2>&1
test
⚡ root@launch-advisor-20191120 ~
>&
is the syntax to redirect a stream to another file descriptor - 0 is stdin, 1 is stdout, and 2 is stderr.
So, in short… 2>
redirects stderr to an (unspecified) file, appending &1
redirects stderr to stdout.
3. Summary
In this post, I demonstrated how to grep with color, the key point is to redirect output of the upstream command to stdout, then you can grep with highlights and color. That’s it, thanks for your reading.