Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk adding color code to text

awk -F: '{ printf  "%-3s %-2s","\n" $1 $2; }'

How do i add in color code? '\e[1;32m'

I try adding in to printf, it give me output of the string instead of color code..

'\e[1;32m' .......
like image 701
user1745860 Avatar asked Jan 23 '13 14:01

user1745860


3 Answers

Try this example:

echo "line 1
line 2" | awk '/line/ {print "\033[32m" $1 "\033[31m" $2 }'

enter image description here

Color is given by "\033[32m"

For Colors:

30 - black   34 - blue          
   31 - red     35 - magenta       
   32 - green   36 - cyan          
   33 - yellow  37 - white     
like image 113
Eduardo Avatar answered Nov 20 '22 08:11

Eduardo


\033[?m properly quoted gives colour:

awk 'BEGIN{ print "\033[34msomething in colour\033[0m";}'

notice how one needs to unescape $1 below:

echo something | awk '{ print "\033[34m"$1" in colour \033[0m";}'
like image 28
bartekbrak Avatar answered Nov 20 '22 10:11

bartekbrak


awk doesn't recognize '\e' as a code for the escape character. Here's a workaround (something more elegant may exist):

# Decimal 27 is the ASCII codepoint for the escape character
awk '{ printf "%c[1;32m foo\n", 27 }' <<<foo
like image 9
chepner Avatar answered Nov 20 '22 09:11

chepner