use warnings; use strict; open my $wfh, '>', 'out.csv' or die $!; my $cols = "Protocol, Source IP Address, Source Port, Data Size\n"; print $wfh $cols; while (){ if (/ (?:.*?\s){3} # get rid of the time (.*?) # capture the proto ($1) \s+ # skip the next whitespace (.*?):(\d+) # separate IP and port, capture both ($2, $3) .*?\( # skip everything until an opening parens (\d+) # capture bytes ($4) /x ){ print $wfh "$1, $2, $3, $4\n"; } } __DATA__ 2016-04-17 10:12:27:682011 GMT tcp 115.239.248.245:1751 -> 192.168.0.17:8080 52976f9f34d5c286ecf70cac6fba4506 04159c6111bca4f83d7d606a617acc5d6a58328d3a631adf3795f66a5d6265f4d1ec99977a5ae8cb2f3133c9503e5086a5f2ac92be196bb0c9a9f653f9669495 (312 bytes) #### # cat out.csv Protocol, Source IP Address, Source Port, Data Size tcp, 115.239.248.245, 1751, 312 #### /(?:.*?\s){3}(.*?)\s+(.*?):(\d+).*?\((.*?)\)$/