?
Solved

Parsing Text of Telnet's Input_log file

Posted on 2004-08-19
8
Medium Priority
?
407 Views
Last Modified: 2008-01-16
I am telneting using Net::Telnet. The Input_log file begins with a Device name, Device date, Device time, and followed with multiple name/value pairs in quotes. The text file contains...

   Device1 04-08-16 00:11:15
   "Rings:7"
   "Bracelets:3"
   "Watches:2"
   Device2 04-08-17 04:12:21
   "Bracelets:1"
   "Watches:22"
   Device3 04-08-18 08:13:53
   "Rings:3"


and I want to parse this text and change the Input_log file to look like...

Device1,04-08-16,00:11:15,Rings,7
Device1,04-08-16,00:11:15,Bracelets,3
Device1,04-08-16,00:11:15,Watches,2
Device2,04-08-17,04:12:21,Bracelets,1
Device2,04-08-17,04:12:21,Watches,22
Device3,04-08-18,08:13:53,Rings,3


Any help would be appreciated.

Thanks,
Steve
0
Comment
Question by:SteveDallas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 1

Accepted Solution

by:
JustinPincar earned 300 total points
ID: 11845669
Easy, if i understand what you want.
Make a script parse.pl

#!/usr/bin/perl
open IN, "Input_log" or die "cant open file";
@buffer=<IN>;
close IN;

@newbuffer="";

foreach $e (@buffer)
{
 if($e!~/^"/)
 {
  $start=$e;
  $start=~s/\s/,/g;
 }
 else
 {
  $e=~tr/:/,/;
  $e=~s/"//g;
  push(@newbuffer, $start.$e);
 }
}

open OUT, ">Input_log" or die "cant open file";
print OUT @newbuffer;
close OUT;

Think that'll do the trick.
~Justin
0
 
LVL 2

Author Comment

by:SteveDallas
ID: 11846137
Justin,

After I run your parse.pl script, Input_log file becomes empty. Can you please double-check your script.

Thanks.
0
 
LVL 2

Author Comment

by:SteveDallas
ID: 11846527
Adding to the above question, what happens if there is not a name/value pair for a particular Device (i.e. Device 4 below)...

...
   Device4 04-08-19 04:15:52
   Device5 04-08-19 18:43:13
   "Rings:8"


In this case, I would only want data off of Device 5 and not Device 4, since it doen't have any name/value pairs...

...
Device5,04-08-19,18:43:13,Rings,8


Thanks in advance.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 18

Assisted Solution

by:kandura
kandura earned 200 total points
ID: 11846800
Hi Steve,

Here's my suggestion. It writes the results to STDOUT, so you can redirect it easily.

#!/usr/bin/perl

my ($device, $date, $time);

while(<>) {
    if( /^(Device\d+)\s+([\d-]+)\s+([\d:]+)$/ ) {
        ($device, $date, $time) = ($1, $2, $3);
        next;
    }

    if( /^"(\w+):(\d+)"$/ ) {
        print join(",", $device, $date, $time, $1, $2), $/;
    }
}

0
 
LVL 1

Expert Comment

by:JustinPincar
ID: 11846932
Not sure what went wrong with mine, mabye permissions problems?

Anyway, kandura's script looks like it would work.
0
 
LVL 2

Author Comment

by:SteveDallas
ID: 11854527
Thanks for the quick responses, but it seems I'm not running the script correctly.

I have the data stored in a file called Input_log. Let's say I call kandura's script above parser.pl. Then to run the script at the prompt, I type "perl parser.pl Input_log". Nothing prints on the screen. I have also tried redirecting the output to a file, and that file becomes empty.

Please let me know my mistake.

Thanks,
Steve
0
 
LVL 18

Expert Comment

by:kandura
ID: 11856081
That could mean your actual data is not the same as what you posted here. I tested it with your sample data, and it worked fine.
0
 
LVL 2

Author Comment

by:SteveDallas
ID: 11856251
I realized my mistake. My input file had leading spaces, while Justin's and kandura's scripts did not account for that.

Thanks again.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question