Solved

Parsing Text of Telnet's Input_log file

Posted on 2004-08-19
8
381 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
  • 4
  • 2
  • 2
8 Comments
 
LVL 1

Accepted Solution

by:
JustinPincar earned 75 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 18

Assisted Solution

by:kandura
kandura earned 50 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

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 …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

821 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