CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Matching a string and printing a variable on the same line

 



joseph4325
Novice

Dec 19, 2012, 5:48 AM

Post #1 of 6 (1657 views)
Matching a string and printing a variable on the same line Can't Post

I'm looking for a script that will match a string like below:

12:30:00 - Error message
12:32:00 - Error message

I want to be able to declare the "Error Message" and have it print the time stamp preceding it for each occurrence.

I thought maybe i could use split but i don't know how to factor in the timestamp variables.


BillKSmith
Veteran

Dec 19, 2012, 9:04 AM

Post #2 of 6 (1652 views)
Re: [joseph4325] Matching a string and printing a variable on the same line [In reply to] Can't Post

You really have not told us enough to help much. Let me make a few guesses. It seems as if you want to read some kind of a log file and display the time stamps of all entries which contain a specified message.

I will assume that you specfy the message by including a key word or phrase on the command line. From your example, I assume that the log file contains nothing but valid entries. Each entry is on one line. Each entry consists of a time-stamp, a space, a hyphen, a space, and the message. Assuming that the log file is called "mylogfile.log", The following code shold get you started.

Code
  
use strict;
use warnings;
my ($message_key) = @ARGV;
open my $LOG_FILE, '<', "mylogfile.log" or die "cannot open log file: 0!";
while (my $entry = <$LOG_FILE>) {
next if $entry !~ /$message_key/;
$entry = s/^([^-]+)\s-\s.*/$1/;
print $entry, "\n";
}


Note: An entry is skipped if it does not contain the key. Otherwise, everything but the time-stamp is deleted, and the remainder of the entry (the time-stamp) is printed.
Good Luck,
Bill


joseph4325
Novice

Dec 22, 2012, 1:49 PM

Post #3 of 6 (1638 views)
Re: [BillKSmith] Matching a string and printing a variable on the same line [In reply to] Can't Post

Thanks for the help. I'm getting the following error with that script:

Use of uninitialized value $_ in substitution (s///)...

this line:
$entry = s/^([^-]+)\s-\s.*/$1/;

Sorry for not providing more info. You're correct it would be reading a log file. Here is an example line:

00:00:07.288 (log text)
00:01:03.499 (log text)


Laurent_R
Veteran / Moderator

Dec 22, 2012, 2:22 PM

Post #4 of 6 (1635 views)
Re: [joseph4325] Matching a string and printing a variable on the same line [In reply to] Can't Post

Hi,

try to change that line to:


Code
$entry =~  s/^([^-]+)\s-\s.*/$1/;



7stud
Enthusiast

Dec 22, 2012, 5:48 PM

Post #5 of 6 (1629 views)
Re: [joseph4325] Matching a string and printing a variable on the same line [In reply to] Can't Post


In Reply To
I thought maybe i could use split but i don't know how to factor in the timestamp variables.

Here is an example line:

00:00:07.288 (log text)
00:01:03.499 (log text)


Try this:


Code
use strict; 
use warnings;
use 5.012;

my $error = "Some Error1";
my @results;

while (my $line = <DATA>) {
if ($line =~ /$error/) {
my @pieces = split " ", $line, 2;
push @results, $pieces[0];
}
}

for (@results) {
say "-->$_<--";
}

__DATA__
00:00:07.288 blah blah Some Error1 blah
00:01:03.499 blah Error2 blah
00:01:05.499 Some Error1 blah
00:01:08.499 Error2 blah



Code
--output:-- 
-->00:00:07.288<--
-->00:01:05.499<--



BillKSmith
Veteran

Dec 22, 2012, 8:27 PM

Post #6 of 6 (1622 views)
Re: [joseph4325] Matching a string and printing a variable on the same line [In reply to] Can't Post

Sorry about the typo in that line. Use Laurent's correction.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives