From: Robert Klemme Date: 2013-06-12T16:19:54+09:00 Subject: Re: extract from output (regular expressions) --047d7b6d937e07bc7b04deefd79c Content-Type: text/plain; charset=ISO-8859-1 On Wed, Jun 12, 2013 at 12:22 AM, Joel Pearson wrote: > FYI the reason I'd use a 2D array for that kind of thing is because you > then have this option for looping through the data: > > output.each do |ip, src_packets, src_bytes, dst_packets, dst_bytes| > #Whatever you want with each set of data... > end > I'd give even more structure to the data: I use a Hash as element type in the Array plus I'd use a Hash on root level to distinguish tables. #!/usr/bin/ruby $w = true require 'ipaddr' data = Hash.new {|h,k| h[k] = []} current = nil ARGF.each_line do |line| case line when /Showing table:\s*(\w+)/ current = data[$1] when /^IP/ h = {} line.scan /(\w+):\s*([\d.]+)/ do |match| key = $1.to_sym val = $2 h[key] = case val when /\A\d+\z/ Integer(val) when /\A\d{1,3}(?:\.\d{1,3}){3}\z/ IPAddr.new val else val end end current << h end end require 'pp' pp data $ ./ip.rb fw.data {"cowboys"=> [{:IP=>#, :packets=>7224498, :bytes=>762829278}, {:IP=>#, :packets=>324472, :bytes=>481821874}, {:IP=>#, :packets=>145748, :bytes=>170332152}, {:IP=>#, :packets=>106100, :bytes=>26420351}, {:IP=>#, :packets=>6948051, :bytes=>1798270528}, {:IP=>#, :packets=>12875, :bytes=>18882957}, {:IP=>#, :packets=>21888, :bytes=>5834302}, {:IP=>#, :packets=>623, :bytes=>177505}, {:IP=>#, :packets=>156, :bytes=>12834}], "indians"=> [{:IP=>#, :packets=>2698261, :bytes=>3740346675}, {:IP=>#, :packets=>300909, :bytes=>136230642}]} In case indentation breaks I put it on github https://gist.github.com/rklemme/5763405 Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --047d7b6d937e07bc7b04deefd79c Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Wed, Jun 12, 2013 at 12:22 AM, Joel Pearson &l= t;lists@ruby-foru= m.com> wrote:
FYI the reason I'd use a 2D array for that kind of thi= ng is because you
then have this option for looping through the data:

output.each do |ip, src_packets, src_bytes, dst_packets, dst_bytes|
=A0 #Whatever you want with each set of data...
end

I'd give even more structure to the data:= I use a Hash as element type in the Array plus I'd use a Hash on root = level to distinguish tables.

#!/usr/bin/ruby
=
$w =3D true

require 'ipaddr'

data =3D Hash.new {|h,k| h[k] =3D []}<= /div>
current =3D nil

ARGF.each_line do |line|
=A0 case line
=A0 when /Showing table:\s*(\= w+)/
=A0 =A0 current =3D data[$1]
=A0 when /^IP/
=A0 = =A0 h =3D {}
=A0 =A0=A0
=A0 =A0 line.scan /(\w+):\s*([\d= .]+)/ do |match|
=A0 =A0 =A0 key =3D $1.to_= sym
=A0 =A0 =A0 val =3D $2

=A0 =A0 =A0 h[key] =3D case val
=A0 =A0 =A0= =A0 when /\A\d+\z/
=A0 =A0 =A0 =A0 Integer= (val)
=A0 =A0 =A0 when /\A\d{1,3}(?:\.\d{1,= 3}){3}\z/
=A0 =A0 =A0 =A0 IPAddr.new val
=A0 =A0 =A0 = else
=A0 =A0 =A0 =A0 val
=A0 =A0 =A0 end
=A0 =A0 end
=A0 =A0=A0
=A0 =A0 current << h
=A0 end
end

require 'pp'
=
pp data


$ ./ip.rb fw.data
{"cowboys"=3D>
=A0 [{:IP=3D>#<IPAddr: IP= v4:192.168.200.1/255.255.2= 55.255>,
=A0 =A0 :packets=3D>7224498,
=A0 =A0 :bytes=3D>7628292= 78},
=A0 =A0{:IP=3D>#<IPAddr: IPv4:192.168.200.2/255.255.255.255>,
= =A0 =A0 :packets=3D>324472,
=A0 =A0 :bytes=3D>481821874},
=A0 =A0{:IP=3D>#<IPAd= dr: IPv4:192.168.200.3/255= .255.255.255>,
=A0 =A0 :packets=3D>145748,
= =A0 =A0 :bytes=3D>170332152},
=A0 =A0{:IP=3D>#<IPAddr: IPv4:192.168.200.4/255.255.255.255>,
=A0 =A0 :pa= ckets=3D>106100,
=A0 =A0 :bytes=3D>26420351},
=A0= =A0{:IP=3D>#<IPAddr: IPv4:192.168.200.5/255.255.255.255>,
=A0 =A0 :packets=3D>6948051,
=A0 =A0 :bytes=3D>1798270= 528},
=A0 =A0{:IP=3D>#<IPAddr: IPv4:192.168.200.6/255.255.255.255>,
= =A0 =A0 :packets=3D>12875,
=A0 =A0 :bytes=3D>18882957},
=A0 =A0{:IP=3D>#<IPAdd= r: IPv4:192.168.200.9/255.= 255.255.255>,
=A0 =A0 :packets=3D>21888,
=A0 = =A0 :bytes=3D>5834302},
=A0 =A0{:IP=3D>#<IPAddr: IPv4:192.168.200.10/255.255.255.255>,
=A0 =A0 := packets=3D>623,
=A0 =A0 :bytes=3D>177505},
=A0 = =A0{:IP=3D>#<IPAddr: IPv4:192.168.200.253/255.255.255.255>,
=A0 =A0 :packets=3D>156,
=A0 =A0 :bytes=3D>12834}],
=A0"indians"=3D>
=A0 [{:IP=3D>#<IPAddr= : IPv4:10.0.0.2/255.255.255.255= >,
=A0 =A0 :packets=3D>2698261,
=A0 =A0 :bytes=3D>3740346675},=
=A0 =A0{:IP=3D>#<IPAddr: IPv4:10.0.0.3/255.255.255.255>,
=A0 =A0 :packet= s=3D>300909,
=A0 =A0 :bytes=3D>136230642}]}

In case indentation breaks I put it on github

Kind regards

robert

--
remember.guy do |as, often| = as.you_can - without end
= http://blog.rubybestpractices.com/
--047d7b6d937e07bc7b04deefd79c--