Bash script in tail -f with colored lines

I tried to create a script from this sentence as follows:

#!/bin/bash if [ $# -eq 0 ]; then tail -f /var/log/mylog.log fi if [ $# -eq 1 ]; then tail -f /var/log/mylog.log | perl -pe 's/.*$1.*/\e[1;31m$&\e[0m/g' fi 

It shows the black tail of the file when I do not pass the arguments to the script, but each line is red when I pass the argument. I would like it to color only lines containing the word passed into the script.

For example, these will be colored lines containing the word β€œinformation”:

 ./color_lines.sh info 

How to change a script to work with one argument?

+4
source share
1 answer

Do not specify an argument variable:

 tail -f input | perl -pe 's/.*'$1'.*/\e[1;31m$&\e[0m/g' 

You can also use grep for this:

 tail -f input | grep -e $1 -e '' --color=always 

and color the whole line with grep:

 tail -f input | grep -e ".*$1.*" -e '' --color=always 
+9
source

Source: https://habr.com/ru/post/1484341/


All Articles