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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split log file into array by blank space for first seven chunks only 2

Status
Not open for further replies.

czarj

Technical User
Apr 22, 2004
130
US
I have a set of logs that looks like this (below). For each line the first seven chunks are the same. I would like to put the file into an array and then split the data first by spaced until I reach chunk 8, which is of a variable length. I would like everything past /vol/vol0/(5) or equivalent to be a single chunk.

dmp Sun Nov 29 10:56:58 IST /vol/vol0/(5) Log_msg(Percentof phase4 time spent for: reading inos 0% dumpinginos 96%)
dmp Sun Nov 29 10:56:58 IST /vol/vol0/(5) Log_msg (Percentof phase4 dump time spent for: wafl read iovec: 54% lev0-ra 0%)
dmp Sun Nov 29 10:56:58 IST /vol/vol0/(5) Log_msg (Phase 4 averages (usec): wafl read iovec time 1902 lev el 0 ra time 219)
dmp Sun Nov 29 10:56:58 IST /vol/vol0/(5) Log_msg (Tape write times (msec): average: 0 max: 1200)
dmp Sun Nov 29 10:56:58 IST /vol/vol0/(5) Log_msg (Tape changes: 1)
dmp Sun Nov 29 10:57:40 IST /vol/INMTACFL00_vol1/(5) Start (Level 0, NDMP)
dmp Sun Nov 29 10:57:40 IST /vol/INMTACFL00_vol1/(5) Options (b=63, u, X)
dmp Sun Nov 29 10:57:40 IST /vol/INMTACFL00_vol1/(5) Snapshot (INBLHCFL07(0101178115)_INMTACFL00_vol1.167 8, Sun Nov 29 10:00:11 IST)
dmp Sun Nov 29 10:57:44 IST /vol/INMTACFL00_vol1/(5) Tape_open (ndmp)
dmp Sun Nov 29 10:57:44 IST /vol/INMTACFL00_vol1/(5) Phase_change (I)
dmp Sun Nov 29 11:03:58 IST /vol/INMTACFL00_vol5/(0) Tape_close (ndmp)


Here I split the file by space, but I don't know how to update teh split function for what I need.

Code:
while (<BACKUPLOG>) {
   chomp;
   my($type,$day,$month,$date,$time,$zone,$volume,$else)=split/ /;
   %bkhash =
   print "$volume,$else\n";

}
close (BACKUPLOG);

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Hi

Code:
[b]while[/b] [teal]([/teal][green][i]<BACKUPLOG>[/i][/green][teal])[/teal] [teal]{[/teal]
 [b]chomp[/b][teal];[/teal]
 [b]my[/b][teal]([/teal][navy]$type[/navy][teal],[/teal][navy]$day[/navy][teal],[/teal][navy]$month[/navy][teal],[/teal][navy]$date[/navy][teal],[/teal][navy]$time[/navy][teal],[/teal][navy]$zone[/navy][teal],[/teal][navy]$volume[/navy][teal],[/teal][navy]$else[/navy][teal])=[/teal][b]split[/b][green][i]/ /[/i][/green][highlight][teal],[/teal][navy]$_[/navy][teal],[/teal][purple]8[/purple][/highlight][teal];[/teal]
 [b]print[/b] [green][i]"$volume,$else\n"[/i][/green][teal];[/teal]
[teal]}[/teal]
Or a dirtier one, but more useful in other circumstances :
Code:
[b]while[/b] [teal]([/teal][green][i]<BACKUPLOG>[/i][/green][teal])[/teal] [teal]{[/teal]
 [b]chomp[/b][teal];[/teal]
 [b]my[/b][teal]([/teal][navy]$type[/navy][teal],[/teal][navy]$day[/navy][teal],[/teal][navy]$month[/navy][teal],[/teal][navy]$date[/navy][teal],[/teal][navy]$time[/navy][teal],[/teal][navy]$zone[/navy][teal],[/teal][navy]$volume[/navy][teal],[/teal][navy][highlight]@[/highlight]else[/navy][teal])=[/teal][b]split[/b][green][i]/ /[/i][/green][teal];[/teal]
 [b]print[/b] [green][i]"$volume,[highlight]@[/highlight]else\n"[/i][/green][teal];[/teal]
[teal]}[/teal]
Personally I would probably prefer something like this :
Code:
[b]while[/b] [teal]([/teal][green][i]<BACKUPLOG>[/i][/green][teal])[/teal] [teal]{[/teal]
  [b]chomp[/b][teal];[/teal]
  [b]my[/b] [navy]@field[/navy][teal]=[/teal][b]split[/b][green][i]/ /[/i][/green][teal];[/teal]
  [b]print[/b] [green][i]"$field[6],@field[7..scalar @field-1]\n"[/i][/green][teal];[/teal]
[teal]}[/teal]

Feherke.
 
works great, thanks

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
One follow-up question. How do I get PERL to spit out each line where one variable in the array equals X? For example, how do I create an output file, hash or new array for each different value of $volume?
In the dataset provided above $volume can equal two values: /vol/vol0/(5) or /vol/INMTACFL00_vol1/(5).

Thanks!


Code:
while (<$BACKUPLOG>) {
 chomp;
 my @logarray = ($type,$day,$month,$date,$time,$zone,$volume,$else)=split/ /,$_,8;

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
depends on how often you are going to writing to them. You could open them both first
open(FILE1, '>/path/to/file1') or die "can't write to /path/to/file1:$!\n";
open(FILE2, '>/path/to/file2') or die "can't write to /path/to/file1:$!\n";


then during your code

if ($volume eq '/vol/vol0/(5)){
print FILE1 "stuff\n";
}
else {
print FILE2 "stuff\n";
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Hi Travis,

I'm not sure how that would work. The example above is very truncated. In reality the log files could have 50+ different volume names and I don't know what the names are. I'm trying to figure out, essentially, how to sort each line containing the same volume name into its own array. Once I have the each coordinate dataset in the individual arrays I will be performing calculations are parsing data from it.

In this example dataset I would have four arrays. One for "/vol/vol0/(5)", one for "/vol/INMTACFL00_vol1/(5)", one for "/vol/INMTACFL00_vol1/(2)" and one for "/vol/INMTACFL00_vol5/(0)."

dmp Sun Nov 29 10:56:58 IST /vol/vol0/(5) Log_msg (Tape write times (msec): average: 0 max: 1200)
dmp Sun Nov 29 10:56:58 IST /vol/vol0/(5) Log_msg (Tape changes: 1)
dmp Sun Nov 29 10:57:40 IST /vol/INMTACFL00_vol1/(5) Start (Level 0, NDMP)
dmp Sun Nov 29 10:57:40 IST /vol/INMTACFL00_vol1/(5) Options (b=63, u, X)
dmp Sun Nov 29 10:57:40 IST /vol/INMTACFL00_vol1/(5) Snapshot (INBLHCFL07(0101178115)_INMTACFL00_vol1.167 8, Sun Nov 29 10:00:11 IST)
dmp Sun Nov 29 10:57:44 IST /vol/INMTACFL00_vol1/(2) Tape_open (ndmp)
dmp Sun Nov 29 10:57:44 IST /vol/INMTACFL00_vol1/(2) Phase_change (I)
dmp Sun Nov 29 11:03:58 IST /vol/INMTACFL00_vol5/(0) Tape_close (ndmp)

Thanks!

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Something like this?

Code:
[gray]#!/usr/bin/perl -w[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%byvolume[/blue][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<DATA>[red])[/red] [red]{[/red]
        [black][b]my[/b][/black][red]([/red][blue]$type[/blue],[blue]$day[/blue],[blue]$month[/blue],[blue]$date[/blue],[blue]$time[/blue],[blue]$zone[/blue],[blue]$volume[/blue],[blue]$else[/blue][red])[/red]=[url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]/[/red][purple] [/purple][red]/[/red],[blue]$_[/blue],[fuchsia]8[/fuchsia][red];[/red]
        [blue]$byvolume[/blue][red]{[/red][blue]$volume[/blue][red]}[/red] .= [blue]$_[/blue][red];[/red]
[red]}[/red]

[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$volume[/blue] [red]([/red][url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [blue]%byvolume[/blue][red])[/red] [red]{[/red]
        [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][purple][b]\n[/b][/purple]Log entries for [blue]$volume[/blue][purple][b]\n[/b][/purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
        [black][b]print[/b][/black] [blue]$byvolume[/blue][red]{[/red][blue]$volume[/blue][red]}[/red][red];[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]dmp Sun Nov 29 10:56:58 IST /vol/vol0/(5) Log_msg(Percentof phase4 time spent for: reading inos 0% dumpinginos 96%)[/teal]
[teal]dmp Sun Nov 29 10:56:58 IST /vol/vol0/(5) Log_msg (Percentof phase4 dump time spent for: wafl read iovec: 54% lev0-ra 0%)[/teal]
[teal]dmp Sun Nov 29 10:56:58 IST /vol/vol0/(5) Log_msg (Phase 4 averages (usec): wafl read iovec time 1902 lev el 0 ra time 219)[/teal]
[teal]dmp Sun Nov 29 10:56:58 IST /vol/vol0/(5) Log_msg (Tape write times (msec): average: 0 max: 1200)[/teal]
[teal]dmp Sun Nov 29 10:56:58 IST /vol/vol0/(5) Log_msg (Tape changes: 1)[/teal]
[teal]dmp Sun Nov 29 10:57:40 IST /vol/INMTACFL00_vol1/(5) Start (Level 0, NDMP)[/teal]
[teal]dmp Sun Nov 29 10:57:40 IST /vol/INMTACFL00_vol1/(5) Options (b=63, u, X)[/teal]
[teal]dmp Sun Nov 29 10:57:40 IST /vol/INMTACFL00_vol1/(5) Snapshot (INBLHCFL07(0101178115)_INMTACFL00_vol1.167 8, Sun Nov 29 10:00:11 IST)[/teal]
[teal]dmp Sun Nov 29 10:57:44 IST /vol/INMTACFL00_vol1/(5) Tape_open (ndmp)[/teal]
[teal]dmp Sun Nov 29 10:57:44 IST /vol/INMTACFL00_vol1/(5) Phase_change (I)[/teal]
[teal]dmp Sun Nov 29 11:03:58 IST /vol/INMTACFL00_vol5/(0) Tape_close (ndmp)[/teal]

Annihilannic.
 
Yeah, that seems to be what I am looking for except for some reason it is not generating any output. Even if I put the same print statement in the WHILE loop.

Code:
#!/usr/software/bin/perl

die "Usage: $0 <filename>\n" unless $#ARGV==0;
open(my $BACKUPLOG, '<', $ARGV[0]) or die "Could not open input: $!\n";

my %byvolume;

while (<BACKUPLOG>) {
        my($type,$day,$month,$date,$time,$zone,$volume,$else)=split/ /,$_,8;
        $byvolume{$volume} .= $_;
}

foreach my $volume (sort keys %byvolume) {
        print "\nLog entries for $volume\n\n";
        print $byvolume{$volume};
}

close (BACKUPLOG);

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Hi

You can not use file handle and variable alternatively.
Code:
[b]open[/b][teal]([/teal][b]my[/b] [navy]$BACKUPLOG[/navy][teal],[/teal] [green][i]'<'[/i][/green][teal],[/teal] [navy]$ARGV[/navy][teal][[/teal][purple]0[/purple][teal]])[/teal] or [b]die[/b] [green][i]"Could not open input: $!\n"[/i][/green][teal];[/teal]

[b]while[/b] [teal]([/teal][green][i]<[highlight]$[/highlight]BACKUPLOG>[/i][/green][teal])[/teal] [teal]{[/teal]
Or
Code:
[b]open[/b][teal]([/teal]BACKUPLOG[teal],[/teal] [green][i]'<'[/i][/green][teal],[/teal] [navy]$ARGV[/navy][teal][[/teal][purple]0[/purple][teal]])[/teal] or [b]die[/b] [green][i]"Could not open input: $!\n"[/i][/green][teal];[/teal]

[b]while[/b] [teal]([/teal][green][i]<BACKUPLOG>[/i][/green][teal])[/teal] [teal]{[/teal]

Feherke.
 
That works. Thanks!!!

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top