Sometimes for these fixed formats (and that doesn't mean "all the time"), it is easier to use split and then an array slice instead of a regex. You only have to select what you need. The first arg to split is a regex, but a simple one. Here is a demo of that. Notice the array index of -2. That is completely fine in Perl and means 2nd from the end. This isn't a good example of this, but I put the vars in the left side of the split into the order that I need them later and adjust in the indices in the array slice.
#!usr/bin/perl
use warnings;
use strict;
my $line = '2016-04-17 10:12:27:682011 GMT tcp 115.239.248.245:1751 ->
+ 192.168.0.17:8080 52976f9f34d5c286ecf70cac6fba4506 04159c6111bca4f83
+d7d606a617acc5d6a58328d3a631adf3795f66a5d6265f4d1ec99977a5ae8cb2f3133
+c9503e5086a5f2ac92be196bb0c9a9f653f9669495 (312 bytes)';
my ($protocol, $ip, $port, $size)= (split /[\s:()]+/,$line)[6,7,8,-2];
print join ",",($protocol, $ip, $port, $size);
print "\n";
__END__
Prints:
tcp,115.239.248.245,1751,312
To test easily to find the index numbers:
my @x = split /[\s:()]+/,$line;
print join "\n", @x;
use the line number from text editor to see indicies without counting
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.