LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-14-2011, 03:51 AM   #1
jack.barnes
LQ Newbie
 
Registered: Mar 2011
Posts: 6

Rep: Reputation: 0
Reading lines from files Perl


Hi All,

The code below is currently reading any line that does not meet my criteria (which is working fine). Instead of printing the line that is incorrect (which it currently does) I wish for it to print the line above it from the file instead of the actual line (the line above has the name which is valuable to the user)

Code:
use Getopt::Long;
use warnings;
use strict;

open my $input, "mirror_status_data";

my $broken_line = '';
my $all_ok=1;

while (<$input>){

    if ( $_ =~ /V Status/ && $_ !~ /available(\/syncd)?$/ ){
	$broken_line = $_;
	$all_ok=0;
	print "badline: $broken_line";
    }   
};

if ($all_ok==0) {
    print "CRITICAL \n";
   # print "badline: $broken_line";
}
else{
    print "OK \n";
}

close $input;
Any Suggestions?
 
Old 04-14-2011, 04:19 AM   #2
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
i'm not familiar with Perl so don't be mad if it doesnt work, but sometimes i need something like you are using (but then in another programming language) and then i do "-1"
i geuss you got to do something like:
Code:
while (<$input>){

    if ( $_ =~ /V Status/ && $_ !~ /available(\/syncd)?$/ ){
	$broken_line = $_ -1;
	$all_ok=0;
	print "badline: $broken_line";
    }   
};
I really don't know if that's possible
or the thing that you put in $broken_line is that a word or a number ?
 
Old 04-15-2011, 03:14 AM   #3
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
I don't know a perl specific solution but here is something that might work
Code:
while (<$input>){


    if ( $_ =~ /V Status/ && $_ !~ /available(\/syncd)?$/ ){
	$broken_line = $_;
	$all_ok=0;
	print "badline: $prev_line";
    }   
$prev_line = $_
}
 
Old 04-15-2011, 04:05 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
There is probably a Perl-centric solution, but brownie's idea and bsat's solution is the way I would go.
Basically you store the previous line on each iteration of the file and when your if is true you print the last stored value.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading lines within a file (Perl) jack.barnes Programming 12 03-16-2011 11:09 AM
Perl alarm not working when reading for a socket , perl seems to be buggy alix123 Programming 1 10-05-2008 07:36 PM
LXer: Reading Native Excel Files in Perl LXer Syndicated Linux News 0 09-05-2008 05:40 PM
LXer: Reading Native Excel Files in Perl LXer Syndicated Linux News 0 09-03-2008 01:10 AM
Perl Script to select common lines in two files. perluser59 Programming 12 05-26-2008 02:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration