Solved

send flow of text to perl

Posted on 2013-12-21
12
362 Views
Last Modified: 2013-12-21
this command  :

 sflowtool -4 -p 3601 | /scripts/DoSTargets

returns


1387652837 178.20.228.47 15 mbps
1387652837 178.20.228.47 1792 pps
1387652837 185.9.156.35 11 mbps
1387652837 185.9.159.233 1536 pps
1387652838 185.9.156.35 11 mbps
1387652839 178.20.228.47 26 mbps
1387652839 178.20.228.47 2560 pps
1387652839 85.100.207.114 8 mbps
1387652840 178.20.228.47 27 mbps
1387652840 178.20.228.47 3072 pps
1387652840 185.9.159.233 12 mbps
1387652840 185.9.159.233 2304 pps
1387652841 178.20.228.47 20 mbps
1387652841 178.20.228.47 2048 pps
1387652841 89.253.155.97 8 mbps
1387652842 178.20.228.47 20 mbps
1387652842 178.20.228.47 1792 pps
1387652843 178.20.228.47 15 mbps
1387652843 178.20.228.47 1792 pps

Open in new window


some outputs like this and how should i read them line by line on perl and split them to
$identifier
$ip
$reason
0
Comment
Question by:3XLcom
  • 6
  • 5
12 Comments
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Ok,

For is this the input line format?
1387652837 178.20.228.47 15 mbps


And then
Identifier is 1387652837
IP is 178.20.228.47
Reason is  15 mbps

Is this correct?? How do you want to see the output?
0
 

Author Comment

by:3XLcom
Comment Utility
yes i am trying to do it


that is what i do :

use strict;
use warnings;


use strict;
use warnings;

open(my $fh, '-|', 'sflowtool -4 -p 3601  | /scripts/DoSTargets') or die $!;
while (my $line = <$fh>) {
    # Do stuff with each $line.
        print  $line;
}
close $fh;

Open in new window



and this is the result :

[root@sflow islemler]# perl deneme.pl
1387662093 178.20.228.85 47 mbps
1387662093 178.20.228.85 4096 pps
1387662093 185.9.159.233 18 mbps
1387662093 185.9.159.233 2816 pps
1387662093 178.20.227.77 9 mbps
1387662094 185.9.159.233 2048 pps
1387662095 178.20.228.85 59 mbps
1387662095 178.20.228.85 5120 pps
1387662095 185.9.159.233 16 mbps
1387662095 185.9.159.233 4096 pps
1387662095 178.20.231.82 1792 pps
1387662095 185.9.156.136 1536 pps



i need to split into variables in  
# Do stuff with each $line.
this part
for ex.
$first
$second
$third
$forth
word should be perfect
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
while ( <$fh> ){
  my($first,$second,$third,$forth) = split;
0
 

Author Comment

by:3XLcom
Comment Utility
i am newbee

use strict;
use warnings;


use strict;
use warnings;

open(my $fh, '-|', 'sflowtool -4 -p 3601  | /scripts/DoSTargets') or die $!;
while (my $line = <$fh>) {
       my($first,$second,$third,$forth) = split;
        print $first;
}
close $fh;

Open in new window


[root@sflow islemler]# perl deneme.pl
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 1.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 1.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 2.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 2.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 3.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 3.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 4.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 4.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 5.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 5.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 6.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 6.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 7.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 7.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 8.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 8.
Use of uninitialized value in split at deneme.pl line 10, <$fh> line 9.
Use of uninitialized value in print at deneme.pl line 11, <$fh> line 9.

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
while ( <$fh> ){
       my($first,$second,$third,$forth) = split;

or

while ( my $line = <$fh> ) {
       my($first,$second,$third,$forth) = split(' ',$line);
0
 

Author Comment

by:3XLcom
Comment Utility
nothing resulted for that


use strict;
use warnings;


use strict;
use warnings;

open(my $fh, '-|', 'sflowtool -4 -p 3601  | /scripts/DoSTargets') or die $!;
while (my $line = <$fh>) {
       my($first,$second,$third,$forth) = split(' ',$line);
        print $first;
}
close $fh;

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 84

Expert Comment

by:ozo
Comment Utility
that would print 138766209313876620931387662093138766209313876620931387662094138766209513876620951387662095138766209513876620951387662095 with no separation between them
0
 

Author Comment

by:3XLcom
Comment Utility
I have changed to other scripts output as this "-" delimiter

[root@sflow islemler]# nano deneme.pl
[root@sflow islemler]# sflowtool -4 -p 3601  | /scripts/DoSTargets
1387664109-37.123.101.178-26-mbps
1387664109-37.123.101.178-2304-pps
1387664109-176.41.215.141-8-mbps
1387664109-185.9.159.233-2048-pps
1387664109-88.253.50.145-8-mbps
1387664109-5.135.155.180-2048-pps
1387664109-185.9.159.233-8-mbps
1387664109-185.9.156.136-1536-pps
1387664110-178.20.227.72-12-mbps
1387664110-178.20.227.72-2048-pps

Open in new window



and my code to :

open(my $fh, '-|', 'sflowtool -4 -p 3601  | /scripts/DoSTargets') or die $!;
while (my $line = <$fh>) {


my($first,$second,$third,$forth) = split("-",$line);
print $first;

}
close $fh;

Open in new window



again empty output
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Again, that would print 1387664109138766410913876641091387664109138766410913876641091387664109138766410913876641101387664110 with no separation and no line terminator
0
 

Author Comment

by:3XLcom
Comment Utility
if i put into for each it works that gives nothing

my @answer = split(' ', $line);
print("$_\n") for (@answer);
0
 

Author Closing Comment

by:3XLcom
Comment Utility
after print i put a "\n" it worked
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
if you want each Identifier printed on a separate line, you could do

$\="\n";
while ( <$fh> ){
       my($first,$second,$third,$forth) = split;
       print $first;
}
or
while ( <$fh> ){
       my($first,$second,$third,$forth) = split;
       print "$first\n";
}
0

Featured Post

Highfive Gives IT Their Time Back

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…

772 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