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


In reply to Re: Parsing { } nested data in a file by NetWallah
in thread Parsing { } nested data in a file by daniel99

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.