Solved

Parsing Text of Telnet's Input_log file

Posted on 2004-08-19
8
363 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
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…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now