perl脚本在linux里,只显示带颜色的命令

perl脚本在linux里,只显示带颜色的命令,第1张

对于Linux管理员来说,grep是日常最常用的命令,可以把匹配的字符输出,同样可以输出颜色

# grep --color 'test' /var/log/maillog

无聊练功:用perl 实现grep --color

# vi colorgrep

#!/usr/bin/perl

use Term::ANSIColor

my $test

if (@ARGV != 2){

die "Please use (colorgrep 'some key word' filename)\n"

}

$test = shift @ARGV

while(<>){

if(/$test/i){

print "$`"

print color "bold red"

print "$&"

print color 'reset'

print "$'"

}

}

这里调用了Term::ANSIColor 函数 color 有以下的参数:

clear, reset, dark, bold, underline, underscore, blink, reverse, concealed, black, red, green, yellow, blue, magenta, cyan, white, on_black, on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, and on_white

还有以下的简化使用方法

2) use Term::ANSIColor qw(:constants)

如果使用这种方法,可以直接把颜色属性放在要输出的问题前面,从而简化输出步骤。这些颜色属性有:

CLEAR, RESET, BOLD, DARK, UNDERLINE, UNDERSCORE, BLINK, REVERSE, CONCEALED, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, ON_BLACK, ON_RED, ON_GREEN, ON_YELLOW, ON_BLUE, ON_MAGENTA, ON_CYAN, and ON_WHITE

等。

例如:

print BOLD BLUE ON_WHITE “Text”, RESET, “\n”

如果你打印完之后想清除掉字符串的格式,一定要记得在最后加上一个RESET的属性值。

例如:

use Term::ANSIColor qw(:constants)

print BOLD, BLUE, “This text is in bold blue.\n”, RESET

3) $Term::ANSIColor::AUTORESET = 1

对于2)的方法,如果你不想在每条打印语句后面加上RESET的话,你可以直接把$Term::ANSIColor::AUTORESET的值设为true。这样每次打印完字符,只要你的属性值之间没有逗号,系统将自动帮你清除掉颜色属性。

看一个完整的例子:

use Term::ANSIColor qw(:constants)

$Term::ANSIColor::AUTORESET = 1

print BOLD BLUE “This text is in bold blue.\n”

print “This text is normal.\n”

这三种方法,使用起来各有千秋,可根据要打印的复杂程度选用。

用C语言Linux下打印带颜色的字符串

字背景颜色范围:40----49 40:黑 41:深红 42:绿 43:黄色 44:蓝色 45:紫色 46:深绿 47:白色

字颜色:30-----------39 30:黑 31:红 32:绿 33:黄 34:蓝色 35:紫色 36:深绿 37:白色

============ANSI控制码的说明 ===============

33[0m 关闭所有属性

33[1m 设置高亮度

33[4m 下划线

33[5m 闪烁

33[7m 反显

33[8m 消隐

33[30m -- 33[37m 设置前景色

33[40m -- 33[47m 设置背景色

33[nA 光标上移n行

33[nB 光标下移n行

33[nC 光标右移n行

33[nD 光标左移n行

33[yxH设置光标位置

33[2J 清屏

33[K 清除从光标到行尾的内容

33[s 保存光标位置

33[u 恢复光标位置

33[?25l 隐藏光标

33[?25h 显示光标

基本格式:

一种控制:printf("\033[xm")

多种控制:printf("\033[xyzm")等价于printf("\033[xm\033[ym\033[zm ")

[root@iZ259r7h4pfZ ~]# printf "%-5s %-10s %-4s\n" NO Name Mark

NOName Mark

[root@iZ259r7h4pfZ ~]# printf "%-5s %-10s %-4.2f\n" 01 Tom 90.3456

01Tom90.35

[root@iZ259r7h4pfZ ~]# printf "%-5s %-10s %-4.2f\n" 02 Jack 89.2345

02Jack 89.23

[root@iZ259r7h4pfZ ~]# printf "%-5s %-10s %-4.2f\n" 03 Jeff 98.4323

03Jeff 98.43

%-5s 格式为左对齐且宽度为5的字符串代替(-表示左对齐),不使用则是又对齐。 %-4.2f 格式为左对齐宽度为4,保留两位小数。

echo -e "\033[30m 黑色字 \033[0m"

echo -e "\033[31m 红色字 \033[0m"

echo -e "\033[32m 绿色字 \033[0m"

echo -e "\033[33m 黄色字 \033[0m"

echo -e "\033[34m 蓝色字 \033[0m"

echo -e "\033[35m 紫色字 \033[0m"

echo -e "\033[36m 天蓝字 \033[0m"

echo -e "\033[37m 白色字 \033[0m"

echo -e "\033[4037m 黑底白字 \033[0m"

echo -e "\033[4137m 红底白字 \033[0m"

echo -e "\033[4237m 绿底白字 \033[0m"

echo -e "\033[4337m 黄底白字 \033[0m"

echo -e "\033[4437m 蓝底白字 \033[0m"

echo -e "\033[4537m 紫底白字 \033[0m"

echo -e "\033[4637m 天蓝底白字 \033[0m"

echo -e "\033[4730m 白底黑字 \033[0m"

控制选项说明 :

\33[0m 关闭所有属性

\33[1m 设置高亮度

\33[4m 下划线

\33[5m 闪烁

\33[7m 反显

\33[8m 消隐

\33[30m -- \33[37m 设置前景色

\33[40m -- \33[47m 设置背景色

\33[nA 光标上移n行

\33[nB 光标下移n行

\33[nC 光标右移n行

\33[nD 光标左移n行

\33[yxH设置光标位置

\33[2J 清屏

\33[K 清除从光标到行尾的内容

\33[s 保存光标位置

\33[u 恢复光标位置

\33[?25l 隐藏光标

\33[?25h 显示光标

合起来一起用

# printf "\033[133m Hello World. \033[0m \n"

Hello World.

# printf "\033[133m %-5s %-10s %-4.2f\033[0m\n"03 Jeff 98.4323

Jeff 98.43230.00


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7316001.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-04
下一篇2023-04-04

发表评论

登录后才能评论

评论列表(0条)

    保存