Awk adding color code to text

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

How to add color code? '\ e [1; 32m '

I am trying to add to printf, it gives me line output instead of color code.

 '\e[1;32m' ....... 
+6
source share
7 answers

awk does not recognize '\ e' as escape character code. This may cause a workaround (something more elegant):

 # Decimal 27 is the ASCII codepoint for the escape character awk '{ printf "%c[1;32m foo\n", 27 }' <<<foo 
+8
source

\033[?m correctly quoted gives the color:

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

let me know how to unescape $1 below:

 echo something | awk '{ print "\033[34m"$1" in colour \033[0m";}' 
+7
source
  awk 'BEGIN{print "^[[1;33mYELLOW"}' 

will print the string YELLOW in yellow (color)

Note first ^[ you need to type ctrl-v , then ESC

I would add a screenshot to show.

enter image description here

The screenshot above showed that it works under zsh and bash.

+2
source

Try this example:

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

enter image description here

The color is specified by the symbol "\033[32m"

For flowers:

 30 - black 34 - blue 31 - red 35 - magenta 32 - green 36 - cyan 33 - yellow 37 - white 
+2
source

Hope this helps anyone looking for an answer!

As you can see, the functions and various initialization allow you to write a fairly simple printed line, as shown below.

 print colour("Red")$1colour("Blue")$0colour("None"); 

For the colour function, you can either pass an integer color value or a name. It accepts parameters in the following formats.

 colour( <attribute> , <background-colour> , <foreground-colour> ) colour( <background-colour> , <foreground-colour> ) colour( <foreground-colour> ) 

You can selectively provide the required parameters.

 function isnumeric(x) { return ( x == x+0 ); } function name_to_number(name, predefined) { if (isnumeric(name)) return name; if (name in predefined) return predefined[name]; return name; } function colour(v1, v2, v3) { if (v3 == "" && v2 == "" && v1 == "") return; if (v3 == "" && v2 == "") return sprintf("%c[%dm", 27, name_to_number(v1, fgcolours)); else if (v3 == "") return sprintf("%c[%d;%dm", 27, name_to_number(v1, bgcolours), name_to_number(v2, fgcolours)); else return sprintf("%c[%d;%d;%dm", 27, name_to_number(v1, attributes), name_to_number(v2, bgcolours), name_to_number(v3, fgcolours)); } BEGIN { # hack to use attributes for just "None" fgcolours["None"] = 0; fgcolours["Black"] = 30; fgcolours["Red"] = 31; fgcolours["Green"] = 32; fgcolours["Yellow"] = 33; fgcolours["Blue"] = 34; fgcolours["Magenta"] = 35; fgcolours["Cyan"] = 36; fgcolours["White"] = 37; bgcolours["Black"] = 40; bgcolours["Red"] = 41; bgcolours["Green"] = 42; bgcolours["Yellow"] = 43; bgcolours["Blue"] = 44; bgcolours["Magenta"] = 45; bgcolours["Cyan"] = 46; bgcolours["White"] = 47; attributes["None"] = 0; attributes["Bold"] = 1; attributes["Underscore"] = 4; attributes["Blink"] = 5; attributes["ReverseVideo"] = 7; attributes["Concealed"] = 8; } { print colour("Red")$1colour("Blue")$0colour("None"); } 
+1
source

Define two variables or the ones you need Example:

 GREENB = "\ 033 [0; 41m" #background color green, Color verde de fondo GREEN = "\ 033 [0; 32m" #Green color text, Color verde en el texto REDB = '\ 033 [0; 41m' #background color red, Color rojo de fondo RED = '\ 033 [0; 31m' #red color text, Color rojo en el texto NORMALB = "\ 033 [0; 49m" #Default background color, Color por defecto de fondo NORMAL = '\ 033 [0m' #Default foreground color, Color por defecto del texto 

awk code: echo "This is a test" | awk -vRojo=${RED} -v NC=${NORMAL} '{print Rojo $1, $2, NC $3, $4}' echo "This is a test" | awk -vRojo=${RED} -v NC=${NORMAL} '{print Rojo $1, $2, NC $3, $4}' echo "This is a test" | awk -vRojo=${RED} -v NC=${NORMAL} '{print Rojo $1, $2, NC $3, $4}' echo "This is a test" | awk -vRojo=${RED} -v NC=${NORMAL} '{print Rojo $1, $2, NC $3, $4}'

0
source
 BDF() { #awk 'BEGIN{ print "\033[34msomething in colour\033[0m";}' #bdf $spool $data $sysout ~ /home/fnsonlh |grep -v avail| awk '{print $5" "$4" "}' | tail -n +2 |tr -d "\012" bdf $spool $data $sysout ~ /home/fnsonlh |grep -v avail| awk '{if($4 > 89) { print "\033[0;31m"$5" "$4"\033[0m " } else { print "\033[0;32m"$5" "$4"\033[0m " }}' |tail -n +2 |tr -d "\012" #"\012 is new line caracter" echo #/usr/bin/w -u uptime } # nishant.chawre@gmail.com 
-2
source

All Articles