阅读背景:

awk将颜色代码添加到文本中

来源:互联网 
awk -F: '{ printf  "%-3s %-2s","\n" 
awk -F: '{ printf  "%-3s %-2s","\n" $1 $2; }'

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

我如何添加颜色代码? '\ E [1;32米'

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

我尝试添加到printf,它给我输出字符串而不是颜色代码..

'\e[1;32m' .......

6 个解决方案

#1


7  

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

awk不会将'\ e'识别为转义字符的代码。这是一种解决方法(可能存在更优雅的东西):

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

#2


7  

\033[?m properly quoted gives colour:

\ 033 [?m正确引用给出颜色:

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

notice how one needs to unescape $1 below:

注意下面需要如何使用$ 1:

echo something | awk '{ print "\033[34m"$1" in colour \033[0m";}'

#3


2  

 awk 'BEGIN{print "^[[1;33mYELLOW"}' 

would print string YELLOW in yellow (color)

会打印黄色的字符串YELLOW(颜色)

NOTE the first ^[ you have to type ctrl-v then ESC

注意第一个^ [你必须输入ctrl-v然后ESC

I would add a screen shot to show.

我会添加一个屏幕截图来显示。

the above screenshot showed it worked under zsh and bash.

上面的截图显示它在zsh和bash下工作。

#4


2  

Try this example:

试试这个例子:

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

Color is given by "\033[32m"

颜色由“\ 033 [32m”给出

For Colors:

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

#5


1  

Hope this helps anyone looking for an answer!

希望这有助于任何寻找答案的人!

As you can see, the functions and various initialization makes it possible to write quite a simple print line as below.

如您所见,功能和各种初始化使得编写如下简单的打印行成为可能。

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

For the colour function, you may either pass in the integer value of colour or the name. It takes params in the below formats.

对于颜色函数,您可以传入颜色的整数值或名称。它采用以下格式的参数。

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

You can selectively give the required params.

您可以有选择地提供所需的参数。

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");
}

#6


-2  

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
}
#[email protected]

; }'



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: