send flow of text to perl

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
3XLcomAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
while ( <$fh> ){
  my($first,$second,$third,$forth) = split;
0
 
farzanjCommented:
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
 
3XLcomAuthor Commented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
3XLcomAuthor Commented:
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
 
ozoCommented:
while ( <$fh> ){
       my($first,$second,$third,$forth) = split;

or

while ( my $line = <$fh> ) {
       my($first,$second,$third,$forth) = split(' ',$line);
0
 
3XLcomAuthor Commented:
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
 
ozoCommented:
that would print 138766209313876620931387662093138766209313876620931387662094138766209513876620951387662095138766209513876620951387662095 with no separation between them
0
 
3XLcomAuthor Commented:
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
 
ozoCommented:
Again, that would print 1387664109138766410913876641091387664109138766410913876641091387664109138766410913876641101387664110 with no separation and no line terminator
0
 
3XLcomAuthor Commented:
if i put into for each it works that gives nothing

my @answer = split(' ', $line);
print("$_\n") for (@answer);
0
 
3XLcomAuthor Commented:
after print i put a "\n" it worked
0
 
ozoCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.