Perl + and Grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl + and Grep
# 1  
Old 11-08-2011
Perl + and Grep

Hi All i have this script that uses glob to look in /var/log/messages.*

Code:
my @messagefiles = glob "/var/log/messages.*";

and the code that uses it is this


Code:
grep  { /NVRM: Xid/ } @messages)


but this spits out this


Code:
/var/log/messages-20111030:Oct 25 13:43:04 brent kernel: NVRM: Xid (0000:03:00): 31, Ch 0000000f, engmask 00000101, intr 10000000
/var/log/messages-20111030:Oct 25 13:43:04 brent kernel: NVRM: Xid (0000:03:00): 31, Ch 00000008, engmask 00000101, intr 10000000
/var/log/messages-20111030:Oct 25 13:43:04 brent kernel: NVRM: Xid (0000:03:00): 31, Ch 00000004, engmask 00000101, intr 10000000
/var/log/messages-20111106:Nov  3 11:57:14 brent kernel: NVRM: Xid (0000:03:00): 8, Channel 00000001
/var/log/messages-20111106:Nov  3 12:48:09 brent kernel: NVRM: Xid (0000:03:00): 32, Channel ID 00000006 intr 00040000
/var/log/messages-20111106:Nov  3 16:56:33 brent kernel: NVRM: Xid (0000:03:00): 31, Ch 00000004, engmask 00000101, intr 10000000
/var/log/messages.all-20111030:Oct 25 13:43:04 brent kernel: NVRM: Xid (0000:03:00): 31, Ch 0000000f, engmask 00000101, intr 10000000
/var/log/messages.all-20111030:Oct 25 13:43:04 brent kernel: NVRM: Xid (0000:03:00): 31, Ch 00000008, engmask 00000101, intr 10000000
/var/log/messages.all-20111030:Oct 25 13:43:04 brent kernel: NVRM: Xid (0000:03:00): 31, Ch 00000004, engmask 00000101, intr 10000000
/var/log/messages.all-20111106:Nov  3 11:57:14 brent kernel: NVRM: Xid (0000:03:00): 8, Channel 00000001
/var/log/messages.all-20111106:Nov  3 12:48:09 brent kernel: NVRM: Xid (0000:03:00): 32, Channel ID 00000006 intr 00040000
/var/log/messages.all-20111106:Nov  3 16:56:33 brent kernel: NVRM: Xid (0000:03:00): 31, Ch 00000004, engmask 00000101, intr 10000000

is there a way to now only get the files that are

Code:
/var/log/messages.all-*

# 2  
Old 11-08-2011
Hi ab52,

What about
Code:
my @messagefiles = glob "/var/log/messages.all-*";

Regards,
Birei
# 3  
Old 11-08-2011
Hi

thanks for the speedy reply

i forgot to say that i tried that, but still get the same result

thanks

Adam
# 4  
Old 11-08-2011
Then, try:
Code:
my @m = grep { /(?i:messages\.all)/ && /(?i:nvrm:\s+xid)/ } @messages;

Regards,
Birei
# 5  
Old 11-08-2011
i tired that but get this errror

Code:
Global symbol "@message" requires explicit package name at ./nvrm line 14.
Global symbol "@messagefiles" requires explicit package name at ./nvrm line 71.
Execution of ./nvrm aborted due to compilation errors.

here is all my code so you can see if i need to chaneg anything esle

Code:
my $OPT = {};
my $PROG     = 'nvrm';
my @m = grep { /(?i:messages\.all)/ && /(?i:nvrm:\s+xid)/ } @message;
#my @messagefiles = glob "/var/log/messages.all-*";
my ($help);
my $prog  = "Nvrm Checker";
my $LOG_FILE = '/tmp/nvrmoutput';

  #ab being urbur lazy and not formating proerply


sub trim($)
{
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}


sub nvrm_norm {

for my $messagefile (@messagefiles){
    if (-e "$messagefile" ) {
        open my $mess_log, $messagefile or die "$PROG: can't open $messagefile: $!\n";
        my @messages = <$mess_log>;
        close $mess_log;
        if (grep  { /NVRM: Xid/ } @messages) {
            print "\n";
        print  " Hostname: $DNHOST\n";
        print  `dnsu dnhinv | grep Serial | cut -d ] -f 2\n `;
        print ' Name: ';
        print  &trim(`dnwho -e  $DNHOST | grep name | cut -d : -f 2 \n \n`);
        print "\n Room Number: ";
              print `cat /tools/SITE/etc/cfg/systems/systems | grep $DNHOST | cut -d , -f 3 | head -n 1 `;
        print " GFX: ";
        print `dnsu dnhinv | grep -i Graphics | sed 's/ driver .*//' | sed 's/.*Graphics: //'\n `;
        print `dnsu dnhinv | grep -i BIOS | cut -d ] -f 2 \n\n`;
        print "\n";
        print "NVRM Errors....\n";
        print `dnsu grep -ir "NVRM: Xid" /var/log/messages* | grep -v "cat /var/log/messages.all | grep 'NVRM: Xid\n"`;
        print "\n";
        if($OPT->{m}) {
                      my $cmd = sprintf `cat /tmp/nvrmoutput | /tools/SITE/etc/bin/dnpost -s '$PROG' -t '$MAILTO' -f '$MAILTO'\n`;
                               }
              
        exit 1 ;
        }
       print ""; 
    }
    }
    
}

# 6  
Old 11-08-2011
I can't guess your intention with the 'grep' in the 'if'. Try to substitute
Code:
if (grep  { /NVRM: Xid/ } @messages) {

with
Code:
if (grep { /(?i:messages\.all)/ && /(?i:nvrm:\s+xid)/ } @messages) {

and keep the rest of the code like the original one.

Regards,
Birei
# 7  
Old 11-09-2011
hum...i changed it as you said by now it does not return anything, the modified code is below

Code:
use strict;
use Data::Dumper;
use Set::Scalar;
use List::Util qw(first);
use Sys::Hostname;
use lib qw(/tools/SITE/perl);
use Getopt::Long 2.11;
use dnutil qw($DNHOST $DNOS $DNUSER);

my $OPT = {};
my $PROG     = 'nvrm';
#my @m = grep { /(?i:messages\.all)/ && /(?i:nvrm:\s+xid)/ } @message;
my @messagefiles = glob "/var/log/messages.all-*";
my ($help);
my $prog  = "Nvrm Checker";
my $LOG_FILE = '/tmp/nvrmoutput';
my $MAILFROM    = 'ab';
my $MAILTO = 'cdt,nvd';

$DNUSER !~ /root/ and die "Run this as root you numpty  \n";


##############################################################
#
# options
#

sub info {
    printf "\n";
    printf "$prog : crappy nvrm search\n";
    printf "\n";
    printf "   -p    pall version ( use sparingly) use -m to mail\n";
    printf "   -c    tea\n";
    printf "   -h    help mode\n";
    printf "\n";
    exit 1;
}



GetOptions($OPT,
    'p|pall'     => \&pall_nvrm,
        'h|help'     => \$help,
    'c|cuppa'    => \&make_tea,
    'm|mail',    
) or die "$prog: Numpty choose a proper option: $!\n";

###############################################################

($help) and info();




# ab being urbur lazy and not formating proerply


sub trim($)
{
    my $string = shift;
    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    return $string;
}


sub nvrm_norm {

for my $messagefile (@messagefiles){
    if (-e "$messagefile" ) {
        open my $mess_log, $messagefile or die "$PROG: can't open $messagefile: $!\n";
        my @messages = <$mess_log>;
        close $mess_log;
        if (grep { /(?i:messages\.all)/ && /(?i:NVRM:\s+xid)/ } @messages) {
            print "\n";
        print  " Hostname: $DNHOST\n";
        print  `dnsu dnhinv | grep Serial | cut -d ] -f 2\n `;
        print ' Name: ';
        print  &trim(`dnwho -e  $DNHOST | grep name | cut -d : -f 2 \n \n`);
        print "\n Room Number: ";
              print `cat /tools/SITE/etc/cfg/systems/systems | grep $DNHOST | cut -d , -f 3 | head -n 1 `;
        print " GFX: ";
        print `dnsu dnhinv | grep -i Graphics | sed 's/ driver .*//' | sed 's/.*Graphics: //'\n `;
        print `dnsu dnhinv | grep -i BIOS | cut -d ] -f 2 \n\n`;
        print "\n";
        print "NVRM Errors....\n";
        print `dnsu grep -ir "NVRM: Xid" /var/log/messages* | grep -v "cat /var/log/messages.all | grep 'NVRM: Xid\n"`;
        print "\n";
        if($OPT->{m}) {
                      my $cmd = sprintf `cat /tmp/nvrmoutput | /tools/SITE/etc/bin/dnpost -s '$PROG' -t '$MAILTO' -f '$MAILTO'\n`;
                               }
              
        exit 1 ;
        }
       print ""; 
    }
    }
    
}

--

---------- Post updated at 07:10 AM ---------- Previous update was at 07:09 AM ----------

Hi birei any throughts

sorry to ask,but i am unsure what i need to change

thanks for all the help

virtual pint coming your way

Last edited by ab52; 11-09-2011 at 08:09 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to grep a pattern in perl?

hello Everyone i am a newbie. i have a file which contains the following E:\gtmproj\script\i486_nt\obj\check_geomtools.exe: o:\portsrc\spg\system_1\i486_nt\advapps\TK-2\objmt\winclockmtq.lib E:\gtmproj\script\i486_nt\obj\check_geomtools.exe:... (12 Replies)
Discussion started by: Rashid Khan
12 Replies

2. Emergency UNIX and Linux Support

Grep inline use perl

Hi, i have input like this : SS-ID VLAN MAC TIME IP RSSI MODE UAPSD BW GI WMOS DHCP IDENTITY ----- ---- --- ---- -- ---- ---- ----- -- -- ---- ---- -------- 1-1 0 C4:46:19:75:C1:55 23m 192.168.5.253 ... (5 Replies)
Discussion started by: justbow
5 Replies

3. Shell Programming and Scripting

Grep in line use perl

Hi, i want to create perl script to telnet and print the output. : This is the script that i have so far : #!/usr/bin/perl use lib '/usr/lib/perl5/5.14'; use Telnet (); $target = "192.168.5.1"; $user = "root"; $passwd = "admin123"; print... (2 Replies)
Discussion started by: justbow
2 Replies

4. Shell Programming and Scripting

Grep in PERL

Hi, Can anybody let me know how this grep will work. The input and output is not known. Also can you give me the details of any link where i can find clearly about grep Thanks in advance (1 Reply)
Discussion started by: irudayaraj
1 Replies

5. Shell Programming and Scripting

perl grep implementation

Hi, I have a huge log file (~5 GB) generated in my server. I have to search through this file for the pattern matching "ERROR" and print only those lines. There is a Perl script available in the server and I have to add a new subroutine so that it will look for this regular expression pattern in... (3 Replies)
Discussion started by: royalibrahim
3 Replies

6. Shell Programming and Scripting

perl instead of grep 'conversion' help?

Hello: I butchered a shell script together, but my script kung-fu is 5 years old and all I've used for 5 years are for i loops. I hope that some perl guru can do in 5 minutes what takes me 5 weeks, so I am asking for help. I have a simple script using "grep -A 2 'string' /path/to/file" but... (4 Replies)
Discussion started by: Habitual
4 Replies

7. Shell Programming and Scripting

grep in perl

Hello I want to grep a line from a file saved in some directory. Can anyone please correct the code below: #!/usr/bin/perl -w $file = "/home/output.txt" $grep_line = "closing zip for topic"; `grep $grep_line* $file`; (1 Reply)
Discussion started by: sureshcisco
1 Replies

8. Shell Programming and Scripting

Perl grep

OK here's the situation: I have got these lines which I have got to parse. If the line contains a particular string and any element from a previously defined array I need to take that particular line and do some further processing. if ((grep(/$_/,$1)) && (grep($pattern,@myarr))) { #Do... (2 Replies)
Discussion started by: King Nothing
2 Replies

9. Shell Programming and Scripting

grep using Perl

I'm using perl to do a grep of each line in a vendor file and find its occurrences in a specific directory. Any values found is saved in @dir. .....(file opened, etc.) .... while ($line=<FILE>){ @dir = `grep $line * `; } It's the specific usage of the system grep that I'm having... (7 Replies)
Discussion started by: gavineq
7 Replies

10. Shell Programming and Scripting

Perl array grep

Hi, I have the following array. @a=( ,); and want to push @a,() if the 6 is not in the $a. This is simplified out of a longer script but im trying for days now and get sick of it. Can any of you help me on this. Cheers Markus (1 Reply)
Discussion started by: elvis00
1 Replies
Login or Register to Ask a Question