下面的脚本是读取文件的每个部分。
文件的每个部分都有一个关键字,需要更改19次。在设置之后,脚本将跳过hte部分的前5行(称为标题或标题),然后计算行数,直到它到达该部分的结尾。
问题是我无法通过循环获取新变量。我已经为变量创建了一个数组,但是当我尝试增加循环和变量时,我没有得到预期的输出。
我将指出这是我的第一个perl脚本,所以如果有可以指出的最佳实践,请这样做。提前谢谢。
#!/usr/bin/perl -w
#use warning;
my $NODE = `uname -n`;
my $a = "/tmp/";
my $b = $NODE;
my $c = "_deco.txt";
my $d = "_deco_mini.txt";
chomp $b;
my $STRING = "$a$b$c";
my $STRING_out = "$a$b$d";
@keyword = (
"Report", "Last", "HP", "sulog",
"sudo", "eTrust", "proftp", "process",
"active clusters", "pdos", "syslog", "BNY",
"syslogmon", "errpt", "ports", "crontab",
"NFS", "scripts", "messages"
);
my $i = 0;
my $jw = "";
my $EOS =
"########################################################################";
my ($qty_lines) = (0);
my $skip5 = 0;
my $skipcnt = 0;
print "Reading File: [$STRING]\n";
if ( -e "$STRING" && open( IN, "$STRING" ) ) {
while (<IN>) {
chomp;
if ( $i < 19 ) {
#while ($i < 19 ) {
#### $_ =~ /$keyword[ $i ]/;
if ( $_ =~ /$keyword[ $i ]/ ) {
print "$keyword[ $i ]\n";
print "$i\n";
$skip5 = 1;
++$i;
print "$i\n";
next;
#}
}
if ($skip5) {
$skipcnt++;
print "SKIP LINE: $_\n";
print "Header LINE: $_\n";
next if $skipcnt <= 5;
$skip5 = 0;
$keeplines = 1;
}
if ($keeplines) {
++$qty_lines;
print "$keyword[ $i ]\n";
last if $_ =~ /$EOS/;
print "KEEP LINE: $_\n";
print "$qty_lines\n";
push @HPLOG, "$_\n";
push @HPLOG, "$qty_lines\n";
}
}
}
else {
print "$STRING: $!\n";
}
close(IN);
open( OUT, ">$STRING_out" );
### print ( OUT "$HPLOG[$#HPLOG] lines in the file\n" ) ;
my $count = $HPLOG[$#HPLOG];
my $outline = 'is the amount of lines in this section';
write(OUT);
format OUT =
This number @<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$count, $outline
.
close(OUT);
我在做这项工作时正在测试各种输出方法。最后,输出文件将邮寄给关键用户。