|
|
Question : Simple perl sort question
|
|
Hi - What I need to do appears to me to be quite a simple task but i'm having difficulties with it. I need to take the contents of a file, and sort the records alphabetically. Logically, I figured I could create an array from the file contents and use a sort on that array but for whatever reason, that does not work. Here is a sample of the file contents:
clmsserver01 MEMORY.MEMORY.MEMPageFreed ALARM:value=285 at 11/04/2003 13:17:23 -- c se1 clmsserver02 MEMORY.MEMORY.MEMPageFreed WARN:value=86 at 11/04/2003 13:18:06 -- cse 1 faxserver01 FILESYSTEM.root.FSCapacity ALARM:value=96 at 11/04/2003 13:18:30 -- uc se1 clmsserver03 FILESYSTEM.proc.FSCapacity ALARM:value=100 at 11/04/2003 13:18:28 -- c se1 webserver01 FILESYSTEM.apps.FSCapacity WARN:value=90 at 11/04/2003 13:18:25 -- uc se1
All I want to do is sort each record alphabetically by system name. Could anyone provide a quick solution to do this?
Thanks!
|
Answer : Simple perl sort question
|
|
don't you want first to create the array in the while loop, sort this array after the while loop and then writing it to a file? [if I my interpretation of your problem is correct ...]
instead of my @list = $final_output; you will have to do: push(@list, $final_output); #add one element to the array
@list = sort @list; # is a correct syntax, but I assume you have to add this after the while loop
The complete script....
#/usr/bin/perl
##### Variable Declarations #####
my $input_file = '/patrol/patrol7/oxhpscripts/patrolnotifyemail_3183.log'; my $unix_alarms = '/patrol/patrol7/oxhpscripts/unix_thresholds.log';
print "Checking log file for Unix Alarming systems...\n\n"; sleep 2; print "Done.\n"; sleep 1;
open (INPUT_FILE, "$input_file") || die ("Cannot open input file, $input_file, for reading.\n", "OS_ERROR: $!\n"); open (UNIX_ALARMS, ">$unix_alarms") || die ("Cannot open output file, $unix_alarms, for writing. \n", "OS_ERROR: $!\n");
my $header = "============================= Patrol Systems in Alarm State ====== ======================\n\n\n"; my $columns = "System Parameter Message Date Time Grp\n"; my @list; while( defined( my $line = ) ) { next if ($line =~ /^\s*$/); # skip blank lines. my ($system, $param, $msg, $at, $date, $time, $dash, $method) = split(/\s+/, $ line); # split next if ($msg =~ /WARN/); # eliminate non-required entries next if ($msg =~ /OK/); next if ($date =~ /04/); next if ($param =~ /itcc.itcc/); my $sep = "------------------------------------------------------------------- ---------------------\n"; my $final_output = sprintf "%-10s %-20s %-15s %-2s %-8s %-8s %-3s %-5s %s\n", $system, $param, $msg, $at, $date, $time, $dash, $method; push(@list, $final_output); #ADD ELEMENT TO ARRAY; # @list = sort @list; MOVE TO THE END #print UNIX_ALARMS $sep, $final_output; #print UNIX_ALARMS $sep, @list; # print UNIX_ALARMS @list; MOVE TO THE END }
@list = sort @list; print UNIX_ALARMS @list;
##### close open files ##### close INPUT_FILE; close UNIX_ALARMS;
sleep 1; exit 0;
|
|
|
|
|