Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-14-2011, 04:51 AM   #1
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)

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 = $_;
	print "badline: $broken_line";

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

close $input;
Any Suggestions?
Old 04-14-2011, 05:19 AM   #2
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:
while (<$input>){

    if ( $_ =~ /V Status/ && $_ !~ /available(\/syncd)?$/ ){
	$broken_line = $_ -1;
	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, 04:14 AM   #3
Registered: Feb 2009
Posts: 347

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

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

Rep: Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943
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.


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 12:09 PM
Perl alarm not working when reading for a socket , perl seems to be buggy alix123 Programming 1 10-05-2008 08:36 PM
LXer: Reading Native Excel Files in Perl LXer Syndicated Linux News 0 09-05-2008 06:40 PM
LXer: Reading Native Excel Files in Perl LXer Syndicated Linux News 0 09-03-2008 02:10 AM
Perl Script to select common lines in two files. perluser59 Programming 12 05-26-2008 03:19 AM > Forums > Linux Forums > Linux - Newbie

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

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