Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Parsing Text of Telnet's Input_log file

Posted on 2004-08-19
8
Medium Priority
?
411 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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

604 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