Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

Parsing Text of Telnet's Input_log file

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
SteveDallas
Asked:
SteveDallas
  • 4
  • 2
  • 2
2 Solutions
 
JustinPincarCommented:
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
 
SteveDallasAuthor Commented:
Justin,

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

Thanks.
0
 
SteveDallasAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kanduraCommented:
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
 
JustinPincarCommented:
Not sure what went wrong with mine, mabye permissions problems?

Anyway, kandura's script looks like it would work.
0
 
SteveDallasAuthor Commented:
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
 
kanduraCommented:
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
 
SteveDallasAuthor Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now