I'm sorry, but I still don't understand you. English is not my first language, maybe it's not your first language either - please try to phrase your problem in longer sentences that do not start with "Else".
I'm trying to rephrase your problem as I interpret it - please correct me if I get it wrong:
- You want to print some stuff to the output file if it does not contain the word "bgs".
- You want that output to into the third line after the word "syslogd" (which occurs only once in the file anyway).
Is that correct?
To attack this problem, let's just reduce all we do to solving this small part and disregard all surrouding stuff. Once we've solved the small part, we can move towards solving the integration into your big program.
For simplicity, let's assume we have the following input file:
This is a test file.
Here comes the line we want: 'syslogd')
You see it?
It was two lines ago.
And before this line we want to output the other stuff.
And from how I interpret your task, this is what you want as output:
This is a test file.
Here comes the line we want: 'syslogd')
You see it?
It was two lines ago.
'bgs')
/usr/bin/su - patrol /usr/adm/best1_default/bgs/scripts/best1collect
-q>>\$LOG 2>>\$LOG
;;
And before this line we want to output the other stuff.
See how writing down what you have and what you want makes the problem much clearer?
Now, on to your code:
First, the line ($line =~ m/'syslogd'\)/);
makes no sense at all. It just checks whether the current line contains "'syslogd')",
but then ignores that knowledge completely.
The line
my $newline = $. + 3;
Only assigns to $newline the number of the current line plus three. It does not advance the current file or anything.
The lines
$newline = $bgs;
print OUT $newline;
next;
print out
'bgs')
/usr/bin/su - patrol /usr/adm/best1_default/bgs/scripts/best1collect
-q>>\$LOG 2>>\$LOG
;;
immediately, instead of waiting some more lines.
You could have told us that directly - by displaying what output you get.
Anyway, take a look at the following program, which reads from the DATA filehandle and see if you understand how it does what it does:
#!/usr/bin/perl -w
use strict;
my $bgs = <<'HERE';
'bgs')
/usr/bin/su - patrol /usr/adm/best1_default/bgs/scripts/best1collect
-q>>$LOG 2>>$LOG
;;
HERE
# Read the whole file into memory. This makes things easier
# for us. Later we can use Tie::File to treat the file
# as an array.
my @lines = <DATA>;
my $saw_bgs;
for my $line (@lines) {
if ($line =~ /bgs/) {
$saw_bgs++;
print "I saw 'bgs' in line >$line<\n";
};
};
if (! $saw_bgs) {
print "I did not see 'bgs' at all.\n";
};
open my $out, ">", 'test.out'
or die "Couldn't create 'test.out': $!";
# Output our file, adding $bgs if necessary
my $print_bgs; # Counter for when to print $bgs
for my $line (@lines) {
if ($line =~ /'syslogd'\)/) {
$print_bgs = 2; # Three lines to go
} elsif (defined $print_bgs and $print_bgs > 0) {
$print_bgs--; # another line done
} elsif (defined $print_bgs and $print_bgs == 0) {
print {$out} $bgs; # Yay, we counted three lines, so now we
+can output $bgs
} else {
# Nothing to do, just a normal line
};
print {$out} $line;
};
__DATA__
This is a test file.
Here comes the line we want: 'syslogd')
You see it?
It was two lines ago.
And before this line we want to output the other stuff.
|