Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Script

Status
Not open for further replies.

YouEnjoyMyself

IS-IT--Management
Joined
Jul 27, 2004
Messages
111
Location
US
A couple of days ago I asked for some help on a scripting. I have now tried modifying that same script to work on another project I am doing, but I keep getting unintialized value error. I believe I have all the values defined. Could someone please explain to me as to why I would get this error. I have posted the script and DATA below. I have tried figuring this out for myself, but I am horrible at scripting. If any one could recomend a good book on scripting that would be helpful too.

#!perl
use strict;
use warnings;

#keywords to look for
my @keywds = ('Client',

);
#construct header from keywds
my $header = join('Client','Backup Time',' Media Date',
map {
(my $x = $_) =~ s/\b([a-z])/\U$1/g; #uc first char of words
$x =~ /^Created|Assigned/? 'Date ' . $x: $x;
} @keywds
);
#make hash with keywds as keys for quick lookup
my %kw;
@kw{@keywds} = ();

my %temp;
my @data;

while (<DATA>) {
chomp;
s/\s+$//;
s{s*\d+:\d+:\d+\s*}{}x; #Takes C time off (see Note)
my ($key, $value) = split (/\:/);
if (exists($kw{$key})) {
$temp{$key} = $value;
if ($key eq $keywds[0]) {
push(@data, { %temp });
%temp = ();
}
}
}
#write the file
my $outfile = "c:\\outfile.csv";
open(CSVFILE, ">$outfile") || die qq(Can't open "$outfile" for output.\n);
print CSVFILE "$header\n";

for my $row (@data) {
for (my $i=0; $i<@keywds; $i++) {
print CSVFILE $row->{$keywds[$i]}, $i<@keywds - 1? ",": "\n";
}
}
close(CSVFILE) || die qq(Can't close output file "$outfile".\n);

__DATA__
Client: albany8-mail2
Backup ID: albany8-mail2_1103590863
Policy: Mail_FileSystem_Albany
Policy Type: MS-Windows-NT (13)
Proxy Client: (none specified)
Creator: root
Name1: (none specified)
Sched Label: Incr
Schedule Type: INCR (1)
Retention Level: 2 weeks (1)
Backup Time: Mon Dec 20 2004 20:01:03 (1103590863)
Elapsed Time: 245 second(s)
Expiration Time: Mon Jan 03 2005 20:01:03 (1104800463)
Compressed: no
Encrypted: no
Kilobytes: 636117
Number of Files: 3650
Number of Copies: 1
Number of Fragments: 1
Histogram: -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
DB Compressed: no
Files File Name: Mail_FileSystem_Albany_1103590863_INCR.f
Previous Backup Files File Name: (none specified)
SW Version: (none specified)
Options: 0x0
MPX: 0
TIR Info: 0
TIR Expiration: Wed Dec 31 1969 19:00:00 (0)
Keyword: (none specified)
Ext Security Info: no
File Restore Raw: no
Image Dump Level: 0
File System Only: no
Object Descriptor: (none specified)
Previous BI Time: Wed Dec 31 1969 19:00:00 (0)
BI Full Time: Wed Dec 31 1969 19:00:00 (0)
Request Pid: 0
Backup Status: 0
Stream Number: 0
Backup Copy: Standard (0)
Files File size: 376031
PFI type: 0
IMAGE_ATTRIBUTE: 0
Primary Copy: 1
Image Type: 0 (Regular)
Job ID: 166680
Num Resumes: 0
Resume Expiration: Wed Dec 31 1969 19:00:00 (0)
Copy number: 1
Fragment: 1
Kilobytes: 636117
Remainder: 0
Media Type: Media Manager (2)
Density: hcart (6)
File Num: 1
ID: 002285
Host: verbackup
Block Size: 65536
Offset: 2
Media Date: Mon Dec 20 2004 20:01:03 (1103590863)
Dev Written On: 2
Flags: 0x0
Media Descriptor: ?
Expiration Time: Mon Jan 03 2005 20:01:03 (1104800463)
MPX: 0
retention_lvl: 2 weeks (1)
checkpoint: 0
resume num: 0

 
My results while i run your script were a file outfile.csv with
Code:
Backup TimeClient Media DateClientClient
            albany8-mail2
but all these, in just two cells A1 and A2

No unintialized value error.

I havent take a look at the code though,
but as long as the error is not comming up.......
 
I don't think though that this is the output you wanted.
 
This should do it. I've bolded changes since the last version.
Code:
#!perl
use strict;
use warnings;

[b]#keywords to look for 
my @keywds = split(/\n/, trim(<<EOF, 'bm'));
    Client
    Backup Time
    Media Date
EOF[/b]

#construct header from keywds
my $header = join(",", 
    [b]#map {
    #    (my $x = $_) =~ s/\b([a-z])/\u$1/g; #uc first char of words
    #    $x =~ /^(Created|Assigned)/? 'Date ' . $x: $x;
    #} [/b]
    @keywds
);

#make hash with keywds as keys for quick lookup
my %kwh;
@kwh{@keywds} = (); 

my %temp;
my @data;

while (<DATA>) {
    chomp;   
    [b]my $line = trim($_);
    $line =~ s{s*\d+:\d+:\d+\s*}{}x; #Takes C time off
    my ($key, $value) = split(/:\s+/, $line);[/b]
    if (exists($kwh{$key})) {
        $temp{$key} = $value;
        if ($key eq $keywds[-1]) {
            push(@data, { %temp });
            %temp = ();
        }
    }
}

#write the file
my $outfile = "c:/work/perl/vmquery.csv";
open(CSVFILE, ">$outfile") || die qq(Can't open "$outfile" for output.\n);
print CSVFILE "$header\n";

for my $row (@data) {
    [b]print CSVFILE join(",", @{$row}{@keywds}), "\n";[/b]
}
close(CSVFILE) || die qq(Can't close output file "$outfile".\n);

[b]sub trim {
    use Carp;
    my %opts = (
        'l'  => sub {s/^\s+//},
        'lm' => sub {s/^\s+//gm},
        'r'  => sub {s/\s+$//},
        'rm' => sub {s/\s+$//gm},
        'b'  => sub {s/^\s+//; s/\s+$//},
        'bm' => sub {s/^\s+//gm; s/\s+$//gm},
    );
    local $_ = shift;
    my $direct = shift || 'b';
    exists($opts{lc($direct)}) 
        ? &{$opts{lc($direct)}}
        : carp qq(Bad option "$direct".\n);
    $_;
}[/b]

__DATA__
[b]Client:            albany8-mail2
Backup ID:         albany8-mail2_1103590863
Policy:            Mail_FileSystem_Albany
Policy Type:       MS-Windows-NT (13)
Proxy Client:      (none specified)
Creator:           root
Name1:             (none specified)
Sched Label:       Incr
Schedule Type:     INCR (1)
Retention Level:   2 weeks (1)
Backup Time:       Mon Dec 20 2004 20:01:03 (1103590863)
Elapsed Time:      245 second(s)
Expiration Time:   Mon Jan 03 2005 20:01:03 (1104800463)
Compressed:        no
Encrypted:         no
Kilobytes:         636117
Number of Files:   3650
Number of Copies:  1
Number of Fragments:   1
Histogram:         -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
DB Compressed:     no
Files File Name:   Mail_FileSystem_Albany_1103590863_INCR.f
Previous Backup Files File Name:   (none specified)
SW Version:        (none specified)
Options:           0x0
MPX:               0
TIR Info:          0
TIR Expiration:    Wed Dec 31 1969 19:00:00 (0)
Keyword:           (none specified)
Ext Security Info: no
File Restore Raw:  no
Image Dump Level:  0
File System Only:  no
Object Descriptor: (none specified)
Previous BI Time:  Wed Dec 31 1969 19:00:00 (0)
BI Full Time:      Wed Dec 31 1969 19:00:00 (0)
Request Pid:       0
Backup Status:     0
Stream Number:     0
Backup Copy:       Standard (0)
Files File size:     376031
PFI type:     0
IMAGE_ATTRIBUTE:     0
Primary Copy:      1
Image Type:        0  (Regular)
Job ID:            166680
Num Resumes:       0
Resume Expiration: Wed Dec 31 1969 19:00:00 (0)
Copy number:       1
 Fragment:         1
 Kilobytes:        636117
 Remainder:        0
 Media Type:       Media Manager (2)
 Density:          hcart (6)
 File Num:         1
 ID:               002285
 Host:             verbackup
 Block Size:       65536
 Offset:           2
 Media Date:       Mon Dec 20 2004 20:01:03 (1103590863)
 Dev Written On:   2
 Flags:            0x0
 Media Descriptor:        ?
 Expiration Time:  Mon Jan 03 2005 20:01:03 (1104800463)
 MPX:              0
 retention_lvl:    2 weeks (1)
 checkpoint:       0
 resume num:       0[/b]
Let me know if this doesn't do it for you.

As for books, have you got "Learning Perl?" Good place to start. After that, "Programming Perl" and "Perl Cookbook."
 
Mike,

Thanks for your help and the recomended books.
 
When I try adding in a keyword that has a duplicate entry in the DATA The script errors out with an unitialized value. I should just be able to add Keywords in as needed right? The keyword I was looking for are:

expiration time
Kilobytes
 
I should just be able to add Keywords in as needed right?
Yes, you should. Just add it to the part of the script that says
Code:
#keywords to look for 
my @keywds = split(/\n/, trim(<<EOF, 'bm'));
    Client
    Backup Time
    Media Date
EOF
If you continue to have problems with it, post the code with any changes you've made along with a sample of the data as you did before, and I'll have a look.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top