awk -F: '{ printf "%-3s %-2s","\n" $1 $2; }'
如何添加颜色代码? '\ e [1; 32m'
我尝试添加到printf,它给我输出字符串而不是颜色代码..
'\e[1;32m' .......
答案 0 :(得分:9)
awk
无法识别'\ e'作为转义字符的代码。这是一种解决方法(可能存在更优雅的东西):
# Decimal 27 is the ASCII codepoint for the escape character
awk '{ printf "%c[1;32m foo\n", 27 }' <<<foo
答案 1 :(得分:9)
\033[?m
给出了颜色:
awk 'BEGIN{ print "\033[34msomething in colour\033[0m";}'
请注意以下内容$1
如何需要unescape:
echo something | awk '{ print "\033[34m"$1" in colour \033[0m";}'
答案 2 :(得分:4)
试试这个例子:
echo "line 1
line 2" | awk '/line/ {print "\033[32m" $1 "\033[31m" $2 }'
颜色由"\033[32m"
对于颜色:
30 - black 34 - blue
31 - red 35 - magenta
32 - green 36 - cyan
33 - yellow 37 - white
答案 3 :(得分:1)
awk 'BEGIN{print "^[[1;33mYELLOW"}'
会以黄色(彩色)打印字符串黄色
注意强>
第一个^[
您需要输入ctrl-v
,然后输入ESC
我会添加一个屏幕截图来显示。
上面的截图显示它在zsh和bash下工作。
答案 4 :(得分:1)
希望这有助于任何寻找答案的人!
正如您所看到的,功能和各种初始化使得编写如下简单的打印行成为可能。
print colour("Red")$1colour("Blue")$0colour("None");
对于colour
函数,您可以传入颜色的整数值或名称。它采用以下格式的参数。
colour( <attribute> , <background-colour> , <foreground-colour> )
colour( <background-colour> , <foreground-colour> )
colour( <foreground-colour> )
您可以有选择地提供所需的参数。
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");
}
答案 5 :(得分:-1)
定义两个或您需要的变量 示例:
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代码:
echo "This is a test" | awk -vRojo=${RED} -v NC=${NORMAL} '{print Rojo $1, $2, NC $3, $4}'