LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-28-2004, 07:46 AM   #1
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Rep: Reputation: 30
Unhappy parsing a logfile in perl


Hi everyone,
I have written a perl script to generate a logfile.
Basically, its like a post-it note, you type what you want to save and it appends it to a logfile.

Here is my current logfile, called '2004':

Code:
stevo@h4x0r:~/.post-it-dir$ cat 2004
2004/04/28, 22:25:18
-----
remember to take out the garbage tonight,
its the recycling and the normal bin
-----
2004/04/28, 22:26:03
-----
Today we have to get this loading of the logfile done
ok?
yes, ok?
are you sure?
I said OK!!!
ok.
-----
2004/04/28, 22:27:14
-----
Someones phone number might be 555-8286 ie 555-UCUM haha!
-----
2004/04/28, 22:27:47
-----
Hey, I just rang it, it really is!
-----
As you can see, each entry begins with a timestamp.

I need to be able to load this file, and then split it up so that each entry in the logfile is distinct (was thinking of each entry in the logfile = an element of an array). Then I can search through it and find the entries that match a certain search string and print each entry out.

But how can I separate each entry? I was thinking of a split on the timestamp, but I need the timestamp to be left in the element of the array. Can you split just before the timestamp? How would I go about reading this logfile into an array?

Thanks!
 
Old 04-29-2004, 03:02 PM   #2
Matt Collier
Member
 
Registered: Apr 2004
Distribution: Debian
Posts: 80

Rep: Reputation: 15
here's one solution, i prefer a hash to an array, it's quicker to index, and you can still treat it as an array with (keys %log).
just as i was finishing this, i realised that probably a better way would be to set $/ (or $INPUT_RECORD_SEPARATOR in the "English" module) to "-----\n"

that way it'd read in the date for the first read, and the entry for the 2nd, but i'd pretty much finished by then


#!/usr/bin/perl -w

use warnings;
use strict;

my $file = '2004';

sub split_log($); #prototype

my %log = &split_log($file);

while (my $key = each(%log)) { print "$key = $log{$key}\n"; }

#--------------------------

sub split_log($) {

my $file = shift or die;
my %hash;

open LOG, $file or die $!;
$/ = undef;
my $log = <LOG>;
close LOG;
$/ = "\n";

my @split = split /\-{5}/, $log;

while (@split) {

my $date = shift @split;
my $entry = shift @split;
last unless $entry;

chomp $date;
chomp $entry;

$date =~ s/^\n//;
$entry =~ s/^\n//;

print "date = $date\n";
print "entry = $entry\n";

$date =~ s|^(\d{4})/(\d{2})/(\d{2}), (\d{2})\d{2})\d{2})|$1$2$3$4$5$6|;

$hash{$date} = $entry;

}

%hash;
}
 
Old 04-30-2004, 03:20 AM   #3
ludeKing
Member
 
Registered: Jul 2003
Location: NSW. Australia
Distribution: Ubuntu, Fedora, Slackware
Posts: 181

Original Poster
Rep: Reputation: 30
Hey thanks,
I used the separator method, setting it to undef so the open file was read all into one string then went from there!
Cheers for your reply!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
URL parsing program in perl ludeKing Programming 3 09-30-2010 03:58 PM
Shell scripting: Print output to logfile, error to logfile & screen stefanlasiewski Programming 18 05-22-2008 12:47 PM
Parsing large text file with perl smaida Programming 5 09-13-2004 04:33 AM
Parsing HTML using Perl smaida Programming 2 05-29-2004 01:20 PM
Perl Parsing filename The Jesus Programming 4 10-03-2003 03:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration