I'm wanting to use the grep
in a bash script to find matching lines in a file, highlight the matches with color, and then print out the results in a table using the column
command. Something like this:
data=`cat file.data | egrep -i --color "$search"`
echo $'\n'"col1"$'\t'"col2"$'\t'"col3"$'\t'"col4"$'\n'"$data" | column -t -s$'\t'
The above code does everything as desired, except that the color is lost.
Here's a simplified example:
As you can see, when I used grep
the results were printed on individual lines and in color, but when I save the results to a variable and then print the variable out, the line breaks and colors are gone.
Is there any way to do what I'm asking?
Indeed, grep returns 0 if it matches, and non-zero if it does not.
In Linux and Unix Systems Grep, short for “global regular expression print”, is a command used in searching and matching text files contained in the regular expressions.
Use the option --color=always
:
data=$(egrep -i --color=always "$search" file.data)
By default, grep
does not produce color unless the output is going directly to a terminal. This is normally a good thing. The option --color=always
overrides that.
For occasions when you don't want color, use --color=never
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With