Check file for being and ending lines for certain text

Posted on 2007-08-06
Last Modified: 2010-03-05
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
Question by:MatthewF
    LVL 8

    Accepted Solution

    open OUT, ">log.txt";   #output log
    open IN, "input.txt"; # input file
    $middle = 0;
       $first = $_;
        print "\n$middle";
      $last = $_;
    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";
    LVL 84

    Expert Comment

    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/;

    Author Comment

    ozo, unless im mistaken yours does not catch an empty file

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now