30. Perl 脚本实战-confgrep

    xiaoxiao2022-07-04  146

    confgrep 命令类似于confview, confgrep 主要用于筛选配置文件中携带关键字的行的信息。 主要特性:

    默认自动过滤空行和注释只保留包含关键字的行有效信息保留原行号显示配置文件总行数, 符合条件的行数

    1. 用法示例

    1.1 查询有效配置行中包含关键字的行

    $ confgrep redis.conf.7481 memory port [13] port 7481 [43] maxmemory 2GB [46] maxmemory-policy noeviction

    1.2 查询所有配置行中包含关键字的行

    -a 选项会筛选包括注释行在内的信息

    $ confgrep -a /usr/local/etc/redis/redis.conf.7481 ip [6 ] # 修改绑定ip Effective Lines: 1; Total Lines: 60

    2. 命令源码

    #!/usr/bin/perl #Desc 提取配置文件中行内容 #Auth zonggf #Date 2016-12-30 use warnings; use Term::ANSIColor qw(:constants); $Term::ANSIColor::AUTORESET = 1; #检查是否是查询帮助 sub check_help{ my $param = $ARGV[0]; $show_all = 0; if("-h" eq $param || "--help" eq $param){ print BOLD BLUE "Desc: "; print "从配置文件中提取包含筛选内容的行, 默认过滤注释行,可添加-a 显示文件所有内容\n"; print BOLD BLUE "Args: "; print "参数1: 配置文件 ; 其它参数: 需要提取的内容\n"; print BOLD BLUE "Exam: "; print "grepcfg redis.conf port client\n"; print BOLD BLUE "Auth: "; print "zonggf\n"; print BOLD BLUE "Date: "; print "2016-12-30\n"; exit; }elsif ("-a" eq $param){ $show_all = 1; shift @ARGV; } } #desc 输出有颜色的字符串 #para1 接收至少两个参数以上,第一个参数为要格式化的字符串,之后的参数为要使用颜色的字符串 sub print_color(){ # 如果长度小于2, 那么不进行格式化输出,直接打印 if(@_ < 2){ print shift @_; return; } #获取要格式化颜色的字符串 my $line = shift @_; #获取要高亮的字符串数组 my @patterns = @_; #获取要高亮显示的字符串数组,拼接正则模式 my $spectors = (shift @_) . '+'; foreach(@_){ $spectors .= "|$_+"; } #按正则模式进行分组 my @arrays = split(/($spectors)/, $line); #输出结果 for my $item(@arrays){ #直接使用@patterns 数组反向匹配,数组内插时,每个字符串直接会有空格 my $pattern = "@patterns"; #字符串中查找元素,如果有的话则高亮显示,否则正常显示 my $idx = index($pattern, $item); if($idx > -1){ print BOLD RED $item; }else{ print $item; } } } #格式化索引长度 #参数: 接收两个参数 # para1: 需要格式化的索引 # para2: 数组长度 #返回: [1 ] sub fmt_idx{ return shift @_ if @_ < 2; my ($str, $array_length) = @_; my $length = length $array_length; return sprintf "[%-${length}s] ", $str; } #################### 主程序 #################### #校验帮助 &check_help; #配置文件名称 $file_name = shift @ARGV; #拼接参数 @patterns = @ARGV; $pattern = shift @ARGV; foreach(@ARGV){ $pattern .= "|$_"; } #读取文件内容,获取文件行数 open $f_cfg, "<", $file_name or die "[错误] $file_name not exists !!\n"; @lines = <$f_cfg>; $lines_length = @lines; #遍历文件内容 $rs_cnt=0; for $idx (1 .. @lines){ $line = $lines[$idx-1]; #匹配条件:符合下面三种情况,则可以输出 #1. 未传入任何筛选条件 #2. 匹配筛选条件,且传入了-a 属性,即show_all 为1, #3. 匹配筛选条件,且未传入-a 选项, 即show_all 为undef,此时过滤注释 if( (!defined $pattern) || $line =~ /$pattern/ && ($show_all ==1 || (!($line=~/^#/))) ){ print BOLD GREEN &fmt_idx($idx, $lines_length); &print_color($line, @patterns); $rs_cnt++; } } #打印配置文件总行数 print BOLD CYAN "\nEffective Lines: $rs_cnt; Total Lines: $lines_length\n"; #关闭文件流 close $f_cfg; ```#!/usr/bin/perl #Desc 提取配置文件中行内容 #Auth zonggf #Date 2016-12-30 use warnings; use Term::ANSIColor qw(:constants); $Term::ANSIColor::AUTORESET = 1; #检查是否是查询帮助 sub check_help{ my $param = $ARGV[0]; $show_all = 0; if("-h" eq $param || "--help" eq $param){ print BOLD BLUE "Desc: "; print "从配置文件中提取包含筛选内容的行, 默认过滤注释行,可添加-a 显示文件所有内容\n"; print BOLD BLUE "Args: "; print "参数1: 配置文件 ; 其它参数: 需要提取的内容\n"; print BOLD BLUE "Exam: "; print "grepcfg redis.conf port client\n"; print BOLD BLUE "Auth: "; print "zonggf\n"; print BOLD BLUE "Date: "; print "2016-12-30\n"; exit; }elsif ("-a" eq $param){ $show_all = 1; shift @ARGV; } } #desc 输出有颜色的字符串 #para1 接收至少两个参数以上,第一个参数为要格式化的字符串,之后的参数为要使用颜色的字符串 sub print_color(){ # 如果长度小于2, 那么不进行格式化输出,直接打印 if(@_ < 2){ print shift @_; return; } #获取要格式化颜色的字符串 my $line = shift @_; #获取要高亮的字符串数组 my @patterns = @_; #获取要高亮显示的字符串数组,拼接正则模式 my $spectors = (shift @_) . '+'; foreach(@_){ $spectors .= "|$_+"; } #按正则模式进行分组 my @arrays = split(/($spectors)/, $line); #输出结果 for my $item(@arrays){ #直接使用@patterns 数组反向匹配,数组内插时,每个字符串直接会有空格 my $pattern = "@patterns"; #字符串中查找元素,如果有的话则高亮显示,否则正常显示 my $idx = index($pattern, $item); if($idx > -1){ print BOLD RED $item; }else{ print $item; } } } #格式化索引长度 #参数: 接收两个参数 # para1: 需要格式化的索引 # para2: 数组长度 #返回: [1 ] sub fmt_idx{ return shift @_ if @_ < 2; my ($str, $array_length) = @_; my $length = length $array_length; return sprintf "[%-${length}s] ", $str; } #################### 主程序 #################### #校验帮助 &check_help; #配置文件名称 $file_name = shift @ARGV; #拼接参数 @patterns = @ARGV; $pattern = shift @ARGV; foreach(@ARGV){ $pattern .= "|$_"; } #读取文件内容,获取文件行数 open $f_cfg, "<", $file_name or die "[错误] $file_name not exists !!\n"; @lines = <$f_cfg>; $lines_length = @lines; #遍历文件内容 $rs_cnt=0; for $idx (1 .. @lines){ $line = $lines[$idx-1]; #匹配条件:符合下面三种情况,则可以输出 #1. 未传入任何筛选条件 #2. 匹配筛选条件,且传入了-a 属性,即show_all 为1, #3. 匹配筛选条件,且未传入-a 选项, 即show_all 为undef,此时过滤注释 if( (!defined $pattern) || $line =~ /$pattern/ && ($show_all ==1 || (!($line=~/^#/))) ){ print BOLD GREEN &fmt_idx($idx, $lines_length); &print_color($line, @patterns); $rs_cnt++; } } #打印配置文件总行数 print BOLD CYAN "\nEffective Lines: $rs_cnt; Total Lines: $lines_length\n"; #关闭文件流 close $f_cfg;
    最新回复(0)