I have the following entry in my logfile.

2016-04-17 10:12:27:682011 GMT tcp 115.239.248.245:1751 -> 192.168.0.17:8080 52976f9f34d5c286ecf70cac6fba4506 04159c6111bca4f83d7d606a617acc5d6a58328d3a631adf3795f66a5d6265f4d1ec99977a5ae8cb2f3133c9503e5086a5f2ac92be196bb0c9a9f653f9669495 (312 bytes)

I want to write a script to split this one line string into pieces in order to write some of these pieces in a .csv file for machine learning. Till now I got this script to find a certain pattern and if found write what it was given to find, hardcoded search. This is not what I want. This is the script I have right now.

#!/usr/bin/perl -w $path1 = "/home/tsec/testwatch/attackerresult.log"; $attacker = ">>/home/tsec/testwatch/attacker.csv"; #$path2 = #$path3 = #$path4 = #function definition #Pattern for attackerlog only sub extractor(){ open(LOG, $path1) or die "Cant't open '$path1': $!"; open(FILE, $attacker) or die "Can't open '$attacker': $!"; $target = "tcp"; while(<LOG>){ if(/$target/){ print FILE $target . "\n"; } } } close(LOG); close(FILE);

I want the output in the CSV file to be something like this:

I can do the csv titles manually

(Titles)Protocol, Source IP Address, Source Port, File Size

(String result from script)tcp, 127.0.0.1, 8080, 312

The above is just an example. Any idea?

In reply to Split string variable of log input and output pieces in text file by firepro20

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.