Is it possible to highlight a search phrase with awk the same way it is using grep?
Given the following file
>gene
AAATTTGCAGAGATTACAGGGGGGG
This grep command $ grep --color=auto GATTACA file.txt
produces
AAATTTGCAGAGATTACAGGGGGGG
where the bold is the colored text.
Because the files I'm actually using have patterns spanning across multiple lines, I'm using awk instead of grep. So instead the files look like this:
> gene
AAATTTGCAGAGAT
TACAGGGGGGG
and I can use the following awk code to print the record with my phrase
awk 'BEGIN{RS=">"; FS="\n";}/GATTACA/{print$0}' file.txt
returning
gene
AAATTTGCAGAGATTACAGGGGGGG
but I would like my pattern to be a color (like the grep cmd):
gene
AAATTTGCAGAGATTACAGGGGGGG
Any help would be greatly appreciated as I'm still very new to unix and awk. This question is not a duplicate of How to print awk's results with different colors for different fields?. This question differs in that it is asking to color a search term and not an entire field. Since technically I'm printing the whole field {print$0}
, my whole returned result changes color.
awk 'BEGIN{RS=">"; FS="\n";}/GATTACA/{print "\033[0;32m"$0"\033[0m"}' file.txt
returns
gene
AAATTTGCAGAGATTACAGGGGGGG
I also tried this:
awk 'BEGIN{RS=">"; FS="\n";}"\033[0;32m"/GATTACA/"\033[0m"{print$0}' file.txt
which just returns the error:
awk: (FILENAME=nametest.txt FNR=1) fatal: division by zero attempted
I'm just not sure how to incorporate the color code into the search term only. It may be that my awk code needs to be entirely reformatted. Please let me know! Thanks again!