Hi Monks, I am writing a script to generate a Array of Hashes by reading a input file which contains the text below
I was not able to generate the data for the interface gif0, Can I please know if there is also a better way to parse this file ?
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384 options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP> inet 127.0.0.1 netmask 0xff000000 inet6 ::1 prefixlen 128 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 nd6 options=201<PERFORMNUD,DAD> gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280 en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 ether f4:0f:24:29:df:4d inet6 fe80::1cb5:1689:1826:cc7b%en0 prefixlen 64 secured scopeid 0 +x4 inet 10.176.85.19 netmask 0xffffff00 broadcast 10.176.85.255 nd6 options=201<PERFORMNUD,DAD> media: autoselect status: active en1: flags=963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX> mtu 1500 options=60<TSO4,TSO6> ether 06:00:58:62:a3:00 media: autoselect <full-duplex> status: inactive p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304 ether 06:0f:24:29:df:4d media: autoselect status: inactive
Below is my code
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; ################ my @AoH; my $rec; my $flag = 0; open my $fh, '<', 'interface.txt' or die; while (<$fh>) { chomp; if (/^(\w+\d{1}):\s+flags=(.*?>)/) { $flag = 1; $rec = {}; $rec->{'interface'} = $1; $rec->{'flags'} = $2; } elsif (/ether\s+(.*$)/) { $rec->{'ether'} = $1; } elsif (/media:\s+(\w+)/) { $rec->{'media'} = $1; } elsif (/inet\s+(\w+\.\w+\.\w+\.\w+)/) { $rec->{'inet'} = $1; } elsif (/status: (\w+)/) { $rec->{'status'} = $1; } elsif ($flag) { push @AoH, $rec; $flag = 0; } } close($fh); print Dumper \@AoH;
Output
./create_arr_of_hashes_from_file.pl $VAR1 = [ { 'flags' => '8049<UP,LOOPBACK,RUNNING,MULTICAST>', 'inet' => '127.0.0.1', 'interface' => 'lo0' }, { 'ether' => 'f4:0f:24:29:df:4d ', 'flags' => '8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTIC +AST>', 'status' => 'active', 'media' => 'autoselect', 'interface' => 'en0', 'inet' => '10.176.85.19' }, { 'status' => 'inactive', 'media' => 'autoselect', 'interface' => 'en1', 'flags' => '963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX +>', 'ether' => '06:00:58:62:a3:00 ' }, { 'status' => 'inactive', 'media' => 'autoselect', 'interface' => 'p2p0', 'flags' => '8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST>', 'ether' => '06:0f:24:29:df:4d ' } ];
In reply to Generation of Array of hashes by reading a file by pr33
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |