[Solved] perl and grep: get a script to look at more then one file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] perl and grep: get a script to look at more then one file
# 1  
Old 09-05-2011
[Solved] perl and grep: get a script to look at more then one file

hi guys

i have this very messy script, that looks in /var/log/messages.all for an error and reports if it finds the key works

how can i get it to look at more then one file, i.e /var/log/message.all *

so it looks in old logs as well

thanks

Code:
exit 0 if (isRenderNode(hostname));
my $PROG     = 'nvrm';
my $xorglog = '/var/log/messages.all';
my $techuser = 'caston|sutton|copdock';




if ($DNHOST !~($techuser)){
if (-e "$xorglog" ) {
    open my $xorg_log, $xorglog or die "$PROG: can't open $xorglog: $!\n";
    my @xorglog = <$xorg_log>;
    close $xorg_log;
    if (grep  { /NVRM: Xid/ } @xorglog) {
        print " Serial Tag: ";
    print `cat /tools/SITE/etc/cfg/systems/systems | grep $DNHOST | cut -d , -f 9 `;
    print " Room Number: ";
          print `cat /tools/SITE/etc/cfg/systems/systems | grep $DNHOST | cut -d , -f 3 `;
    print `dnsu dnhinv | grep -i Graphics| cut -d : -f 2\n `;
    print `dnsu dnhinv | grep -i BIOS | cut -d ] -f 2 \n`;
    print `cat $xorglog | grep "NVRM: Xid"`;
        }
   # XXX more... 
}
}

# 2  
Old 09-05-2011
can you post some sample input file contents and the output format
# 3  
Old 09-05-2011
if i remove all the custome things ( i.e the custome print command which rely on other scripts"

it should grep /var/log/messages.all for the phrase NVRM: Xid

and output the print commands

i would like it to look in all the message.all * files i,e

/var/log/messages.all-20110731
/var/log/messages.all-20110904
/var/log/messages.all-20110814
# 4  
Old 09-05-2011
Quote:
Originally Posted by ab52
...
how can i get it to look at more then one file, i.e /var/log/message.all *
so it looks in old logs as well
Code:
exit 0 if (isRenderNode(hostname));
my $PROG     = 'nvrm';
my $xorglog = '/var/log/messages.all';
my $techuser = 'caston|sutton|copdock';

if ($DNHOST !~($techuser)){
if (-e "$xorglog" ) {
    open my $xorg_log, $xorglog or die "$PROG: can't open $xorglog: $!\n";
    my @xorglog = <$xorg_log>;
    close $xorg_log;
    if (grep  { /NVRM: Xid/ } @xorglog) {
        print " Serial Tag: ";
    print `cat /tools/SITE/etc/cfg/systems/systems | grep $DNHOST | cut -d , -f 9 `;
    print " Room Number: ";
          print `cat /tools/SITE/etc/cfg/systems/systems | grep $DNHOST | cut -d , -f 3 `;
    print `dnsu dnhinv | grep -i Graphics| cut -d : -f 2\n `;
    print `dnsu dnhinv | grep -i BIOS | cut -d ] -f 2 \n`;
    print `cat $xorglog | grep "NVRM: Xid"`;
        }
   # XXX more... 
}
}

Try this (changes are in red):

Code:
exit 0 if (isRenderNode(hostname));
my $PROG     = 'nvrm';
# my $xorglog = '/var/log/messages.all';  ## comment out or delete this line
my $techuser = 'caston|sutton|copdock';

if ($DNHOST !~($techuser)){
foreach my $xorglog (glob "/var/log/messages.all-\d{8}") { # $xorglog is one file name at a time; the glob regex could be tailored to suit your need
if (-e "$xorglog" ) {
    open my $xorg_log, $xorglog or die "$PROG: can't open $xorglog: $!\n";
    my @xorglog = <$xorg_log>;
    close $xorg_log;
    if (grep  { /NVRM: Xid/ } @xorglog) {
        print " Serial Tag: ";
    print `cat /tools/SITE/etc/cfg/systems/systems | grep $DNHOST | cut -d , -f 9 `;
    print " Room Number: ";
          print `cat /tools/SITE/etc/cfg/systems/systems | grep $DNHOST | cut -d , -f 3 `;
    print `dnsu dnhinv | grep -i Graphics| cut -d : -f 2\n `;
    print `dnsu dnhinv | grep -i BIOS | cut -d ] -f 2 \n`;
    print `cat $xorglog | grep "NVRM: Xid"`;
        }
   # XXX more... 
}
}   # closing brace for the foreach loop
}

# 5  
Old 09-06-2011
worked like a charm, i had to include an exit and then end otherwise is looped and printed out all the information more then once
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] Grep in bash script

Hi Experts, I'm writing script to find out last files and its modified date - unfortunately am having problem with the below script. Error message: "grep: sales.txt: No such file or directory" #!/bin/bash var=1 var1=`awk '{n++} END {print n}' sales.txt` while ] do prod=$var... (6 Replies)
Discussion started by: parpaa
6 Replies

2. UNIX for Advanced & Expert Users

[SOLVED] Grep IP's from xml with name of file

Hi all, actually i have a lot of IP addresses on multiple .xml files on my folder, my tag on xml files are like this, <db-url>jdbc:oracle:thin:@10.100.5.56:1521:DWH01</db-url> i actually can print only the IP addresses awk -F"" '/url/ { print $3 }' *.xml 10.100.5.56 i would like to grep... (2 Replies)
Discussion started by: charli1
2 Replies

3. Shell Programming and Scripting

[Solved] Help with grep script

Hi, I'm having trouble with a script to copy one line out of multiple files in a directory and copy to a file called test. I've tried the code below but it copies one line out of the first file multiple times not one line out of all the files. Would someone help? I'm very new to all this. ... (8 Replies)
Discussion started by: bob101
8 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Perl Question - split function with csv file

Hi all, I have a csv file that appears as follows: ,2013/03/26,2012/12/26,4,1,"2017/09/26,5.75%","2017/09/26,1,2018/09/26,1,2019/09/26,1,2020/09/26,1,2021/09/26,1",,,2012/12/26,now when i use the split function like this: my @f = split/,/; the split function will split the data that is... (2 Replies)
Discussion started by: WongSifu
2 Replies

5. Programming

Unix grep in perl script

Hello, Fairly simple really I have an xml file and I want to check to see if it contains a pattern. The pattern is "../" On the command line I can type: grep "\.\./" myFile.xml and I get desired result. To do the same thing in a perl script I thought it was as simple as putting the ``... (4 Replies)
Discussion started by: Jaymoney
4 Replies

6. Shell Programming and Scripting

Run perl command in script[solved]

Hi all, When I put the Perl command in a script, I got error. system("perl -pi -e 's@words@words@g' myFile"); The error is: Unrecognized character \x8A; marked by <-- HERE after دت مد�<-- HERE near column 15 at -e line 1. Thanks in advance. ---------- Post updated at 06:30 AM... (0 Replies)
Discussion started by: Lham
0 Replies

7. UNIX for Dummies Questions & Answers

SOLVED: Text file compare using perl

I have two text file.... One text file contain in this format...... keyvalue.txt \SUM\SUM_cam.c \SUM\SUM_cam.h \SUM\SUM_command.c \SUM\SUM_command.h \SUM\SUM_dab.c \SUM\SUM_dmb.c \SUM\SUM_eventHandler.h \SUM\SUM_eventHandler_dab.c \SUM\SUM_eventHandler_dmb.c ... (6 Replies)
Discussion started by: suvenduperl
6 Replies

8. Shell Programming and Scripting

SOLVED: reading config file in a perl script

Hi! I have a need to do this in Perl. script.pl -config file The script would be doing a wget/LWP on a URL which is defined in the config file. So when I run the script it should return either one of these conditions - 1) OK with exit status 0. Should also print "wget URL" 2)... (6 Replies)
Discussion started by: jacki
6 Replies

9. Shell Programming and Scripting

[Solved] how Execute a Perl Script from any location on the machine

hi guys, i have a query, i wrote a perl script that will collect cpu statistics Scripts/myScripts/ cpu.pl i want to make this file so that i can run it from any location on any drive, without giving the path explicitly. like $/home/ravi/perl cpu.pl i tried this... (8 Replies)
Discussion started by: niteesh_!7
8 Replies

10. Shell Programming and Scripting

grep ^M in file using perl script....

hi i am using perl on windows ( active state perl 5.8 ) and i want to check for Control-M (^M) in file. files to be checked are in unix format so new line character is (\n). This perl script is called from Batch file ( windows .BAT file ) my script is while (<PROGRAM>) { ... (12 Replies)
Discussion started by: zedex
12 Replies
Login or Register to Ask a Question