in reply to Parsing { } nested data in a file

Here you go:
use strict; use warnings; local $/="}\n"; my ($currentitem, %item); while (<>){ #print "---$_;;;\n"; for my $line (split "\n"){ if ($currentitem){ if ( $line=~ m/^\s*(\S+)\s*\{\s*$/){ #ignore "destination-translation"# ; }elsif ( $line=~/(\S+)\s+(\S+)\s*;/){ $item{$currentitem}{$1}=$2; } }elsif( $line=~ m/^\s*(\S+)\s*\{\s*$/){ $currentitem = $1 } } $currentitem=undef; } for my $name (sort keys %item){ print qq| \{:corename => "$name", :pubip => "$item{$name}{destinati +on}", :privip => "$item{$name}{'translated-address'}"},\n|; }
Run:
>perl 1213915.pl YourFileName.txt {:corename => "Prod1_Core_10.2.6.1", :pubip => "8.39.63.3", :privip + => "10.2.6.1"}, {:corename => "Prod1_Core_10.2.6.2", :pubip => "8.39.63.4", :privip + => "10.2.6.2"},
Note: This depends on the format of the file having consistent braces. Not the smartest parser - just a hack.

                Memory fault   --   brain fried

Replies are listed 'Best First'.
Re^2: Parsing { } nested data in a file
by daniel99 (Initiate) on May 02, 2018 at 17:33 UTC
    Beautiful hack! thanks so much NetWallah :)