One way:

c:\@Work\Perl\monks>perl -wMstrict -e "my $data = qq{cfg_a\ncfg_b[2:0]\ncfg_c[4:0]\ncfg_d\n}; print qq{[[$data]] \n\n}; ;; open my $fh, '<', \$data or die qq{opening string by reference: $!}; ;; my $identifier = qr{ [[:alpha:]] [_[:alpha:]]* }xms; my $bracket = qr{ \[ \d+ : \d+ \] }xms; ;; while (my $line = <$fh>) { my $matched = my ($id, $br) = $line =~ m{ \A ($identifier) ($bracket?) \Z }xmsg; die qq{bad line: '$line'} unless $matched; printf qq{logic%s%s\n}, map { $_ ? qq{ $_} : '' } $br, $id; } " [[cfg_a cfg_b[2:0] cfg_c[4:0] cfg_d ]] logic cfg_a logic [2:0] cfg_b logic [4:0] cfg_c logic cfg_d

Update: Don't worry about the odd looking

my $data= qq{cfg_a\ncfg_b[2:0]\ncfg_c[4:0]\ncfg_d\n}; ... ... open my $fh, '<', \$data or die qq{opening string by reference: $!};
at the start. That's just a quick way to generate an example. I assume you know how to open and read a file in the more usual way. Also, please see perlre, perlretut, and perlrequick.

Update 2: Actually, the  /g modifier in the  m// above is redundant (but harmless). This
    my $matched = my ($id, $br) = $line =~ m{ ... }xms;
works just as well.


Give a man a fish:  <%-{-{-{-<


In reply to Re: To find and replace in a file by AnomalousMonk
in thread To find and replace in a file by suvendra

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.