LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


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

Rep: Reputation: 0
Reading lines within a file (Perl)


Hi All,

I am trying to read certain lines within a file and give the output of the certain lines that dont equal my value, I think showing you would be easier.

There is multiples of these inside one file.......
Code:
LV Name                     /dev/vg00/lvol1
LV Status                   available/syncd
LV Size (Mbytes)            300
Current LE                  75
Allocated PE                75
Used PV                     1

LV Name                     /dev/vg00/lvol2
LV Status                   available/syncd
etc...
I want to read everything in the file, if the status is not available then it should display the name (directly above status). If they are all availbale then do nothing.

I think I know how to do it which includes putting the info in string form and placing in hash but it is proving to be out of my skill range.

Any help is accepted!

Last edited by jack.barnes; 03-16-2011 at 04:40 AM.
 
Old 03-16-2011, 06:15 AM   #2
scoban
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: Slackware
Posts: 145

Rep: Reputation: 16
grep -B 1 "not available" file_name
 
Old 03-16-2011, 06:24 AM   #3
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
@scoban - I have two issues with your solution:

1. It will return both lines, ie not just the one prior.
2. What if 'not available' is not linked against status?

@OP - Does it have to be Perl? If so, what have you tried so far?
 
Old 03-16-2011, 06:24 AM   #4
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Ruby(1.9+)
Code:
$ ruby -ane 's=$_.gsub(/LV Name\s+/,"") if /LV Name/; print s if /LV Status/ && !/available/ ' file
 
Old 03-16-2011, 06:49 AM   #5
jack.barnes
LQ Newbie
 
Registered: Mar 2011
Posts: 6

Original Poster
Rep: Reputation: 0
Grail - I am afraid the nature of the work has to be written in perl and it is actually a nagios plugin I am creating (believe it or not the nagios part of it is the easiest)

After banging my head against the wall for days, I have come up with nothing, not helpful I am aware. The following things I have down but all jumbled up just so I know what im doing!

Code:
cat mirror_status_data #diplays data
open my $output_fh, "******"
    
while (<$output_fh>){
        my $line=$_;
	$line=~s/\s+//g;
You wouldnt believe how confused I am!!!
 
Old 03-16-2011, 06:58 AM   #6
scoban
Member
 
Registered: Nov 2004
Location: Turkey
Distribution: Slackware
Posts: 145

Rep: Reputation: 16
I am assuming "not available" will shown in status line only.
Code:
grep -B 1 "not available" file_name | head -1
removes second line.

Can be called from perl using system() I think..
 
Old 03-16-2011, 07:39 AM   #7
fogpipe
Member
 
Registered: Mar 2011
Distribution: Slackware 64 -current,
Posts: 550

Rep: Reputation: 196Reputation: 196
Maybe this will be of help

http://perldoc.perl.org/functions/readline.html
 
Old 03-16-2011, 08:05 AM   #8
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
Well I am a bit of a Perl noob, but something like this might lead you in the right direction:
Code:
#!/usr/bin/perl -w

open my $input, 'your_file_name_here';

my $keep = '';

while (<$input>)
{
    if ( $_ =~ /Status.*not available/)
    {
        print $keep;
    }
    else
    {
        $keep = $_;
    }
};
 
Old 03-16-2011, 08:52 AM   #9
jack.barnes
LQ Newbie
 
Registered: Mar 2011
Posts: 6

Original Poster
Rep: Reputation: 0
Everything has been pretty helpfull so far, but im not there yet, unfortunately.

The status varies between different things, so I am after anything that is not "available" or "available/synced". Wherever this occures in the file I want to display the line above it else print "ok"
 
Old 03-16-2011, 09:24 AM   #10
jack.barnes
LQ Newbie
 
Registered: Mar 2011
Posts: 6

Original Poster
Rep: Reputation: 0
This is what I have so far.

Code:
#!/usr/bin/perl -w

#check_mirror_statuss
#This plugin will allow the user to check the mirror status' and give a CRITICAL message if the status is not available.
#Author: Jack Barnes
#Version: v1.0
#This program is free software. You may copy or redistribute it under the same terms as Perl itself.

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

my $file_name = "MIRROR_STATUS";

sub main {

#Counting number of current processes
    open my $output_fh, 'mirror_status_data';

#Searching file
    while (<$output_fh>){
    
#Deciding if Warning or Critical message is needed 
	if ("***LV status != available"){
	    print "****lv name";
	    print "$file_name CRITICAL - mirror status = ***LV name"; 
	    exit;
	}
	else{
	    print "$file_name OK";
	    exit;
	}
    } 
}

main ();
I have placed **** where the code needs to go, but its got me a little confuffled!

Last edited by jack.barnes; 03-16-2011 at 09:29 AM.
 
Old 03-16-2011, 10:30 AM   #11
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
So I find it a little hard to follow your logic based on the variable names, ie you have a variable called file_name but it isn't used when you open the file but is used for the message??

But using your current data, the following seems to work for me:
Code:
#!/usr/bin/perl -w

open my $input, f2;

my $keep = '';

while (<$input>)
{
    if ( $_ =~ /Status/ && $_ !~ /available(\/synced)?$/ )
    {
        print $keep;
    }
    else
    {
        $keep = $_;
    }
};

close $input
I am guessing the 'if' is the most important part for you as the rest is fairly standard stuff.
 
1 members found this post helpful.
Old 03-16-2011, 10:49 AM   #12
jack.barnes
LQ Newbie
 
Registered: Mar 2011
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks grail, I have played around with that to my specifications and it pretty much what I was after!

In regards to
Quote:
you have a variable called file_name but it isn't used when you open the file but is used for the message??
this was to save typing out the name of the file continusly which is what I had to do in my last nagios plugin (Im a copy paste type of guy).

Now I have that out of the way i will try and finish this plugin off without posting for help.

Thanks Again
 
Old 03-16-2011, 11:09 AM   #13
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
Hey, don't be shy to ask for help ... sheesh not sure where I would be if I didn't

Glad was able to help you onto the right track
 
  


Reply



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 specific lines from a file Mork2k4 Programming 2 09-10-2009 11:51 PM
Perl reading a file umbrella2 Programming 18 02-04-2009 05:18 PM
How to modify a field in few lines in a file and save the new file - in Perl rounak94 Programming 1 10-02-2008 07:43 PM
PHP - Help With Reading Lines In A File windisch Programming 6 05-03-2006 12:48 PM
Reading the number of Lines in a File Mistro116@yahoo.com Programming 31 11-24-2005 12:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:16 AM.

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