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. 命令源码
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;
}
}
sub print_color(){
if(@_ < 2){
print shift
@_;
return
;
}
my $line = shift
@_;
my @patterns = @_;
my $spectors = (shift
@_) . '+';
foreach(@_){
$spectors .= "|$_+";
}
my @arrays = split
(/($spectors)/, $line);
for my $item(@arrays){
my $pattern = "@patterns";
my $idx = index
($pattern, $item);
if($idx > -1){
print BOLD RED
$item;
}else{
print $item;
}
}
}
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];
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;
```
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;
}
}
sub print_color(){
if(@_ < 2){
print shift
@_;
return
;
}
my $line = shift
@_;
my @patterns = @_;
my $spectors = (shift
@_) . '+';
foreach(@_){
$spectors .= "|$_+";
}
my @arrays = split
(/($spectors)/, $line);
for my $item(@arrays){
my $pattern = "@patterns";
my $idx = index
($pattern, $item);
if($idx > -1){
print BOLD RED
$item;
}else{
print $item;
}
}
}
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];
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;