in reply to To find and replace in a file
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
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). Thismy $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: $!};
Give a man a fish: <%-{-{-{-<
|
|---|