[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

Check file for being and ending lines for certain text

I need a quick script that will check three things and write out to log if any are not correct:

Checks that the FIRST  line of a file begins with the characters   FGD
Checks that the LAST line of a file begins with the characters   FQD

Checks that there are lines between the FIRST and LAST lines, not just carriage returns
0
MatthewF
Asked:
MatthewF
1 Solution
 
nedfineCommented:
open OUT, ">log.txt";   #output log
open IN, "input.txt"; # input file
$i=0;
$middle = 0;
while(<IN>)
{
  if($i==0)
  {
   $first = $_;
  }
  else
  {
   if($_=~/\S+/)
   {
    $middle++;
    print "\n$middle";
   }
  }
  $i++;
  $last = $_;
 
}
$middle--;
print "\n$middle";
if($first !~/^FGD/)
{
 print OUT "\n first line does not start with FGD";
}
if($last !~/^FQD/)
{
 print OUT "\n Last line does not start with FQD";
}
if($middle == 0)
{
 print OUT "\n no valid data in between first and last line";
}
0
 
ozoCommented:
that fails to checks that there are lines between the FIRST and LAST lines when there is only one line in the file



open IN, "input.txt" or die $!; # input file
open OUT, ">log.txt" or die $!;   #output log
my $middle = 0;
while( <IN> ){
    if( eof ){
       print OUT "no valid data in between first and last line\n" unless $middle;
       print OUT "Last line does not start with FQD\n" unless /^FQD/;
    }elsif( 2..eof ){
        $middle += /\S/;
    }
    print OUT "first line does not start with FGD\n" if 1..1 and !/^FGD/;
}
0
 
MatthewFAuthor Commented:
ozo, unless im mistaken yours does not catch an empty file
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now