我想使用或命令将特定的内容转换Verilog Bus
为单独的分割形式。sed
awk
输入
module test ( temp_bus[3:0], temp_B[1:0] )
input [3:0] temp_bus;
output [1:0] temp_B;
endmodule
输出
module test ( temp_bus[3], temp_bus[2], temp_bus[1], temp_bus[0], temp_B[1], temp_B[0])
input temp_bus[3], temp_bus[2], temp_bus[1], temp_bus[0];
output temp_B[1], temp_B[0];
endmodule
Edit1:具有多个声明的情况
module test ( temp_bus[3:0], temp_B[1:0] , temp_C[1:0] )
input [3:0] temp_bus;
output [1:0] temp_B , temp_c;
endmodule
结果必须有
output temp_B[1], temp_B[0], temp_C[1], temp_C[0] ;
CAS几乎已经给出了最佳解决方案。
答案1
这是一种方法perl
:
(修订版将处理您的两个示例输入。它看起来也像里面的分号[]
不会混淆 Markdown 语法突出显示)
#! /usr/bin/perl
use strict;
sub expand {
my ($name,$start,$stop) = @_;
my $step = ( $start < $stop ? 1 : -1);
my @names=();
my $i = $start;
while ($i ne $stop + $step) {
push @names, "$name\[$i\]";
$i += $step;
}
return @names;
};
while(<>) {
chomp;
s/([(),;])/ $1/g; # add a space before any commas, semi-colons, and
# parentheses, so they get split into separate fields.
my @l=(); # array to hold the output line as it's being built
my @line = split ; # split input line into fields, with 1-or-more
# whitespace characters (spaces or tabs) between each
# field.
my $f=0; # field counter
while ($f < @line) {
if ( $line[$f] =~ m/module/io ) {
push @l,$line[$f++];
while ($f < @line) {
if ( $line[$f] =~ m/^(.*)\[(\d+):(\d+)\]$/o ) {
# expand [n:n] on module line
push @l, join(", ",expand($1,$2,$3));
} else {
push @l, $line[$f]
};
$f++;
};
} elsif ($line[$f] =~ m/^(?:input|output)$/io) {
# use sprintf() to indent first field to 10 chars wide.
$line[$f] = sprintf("%10s",$line[$f]);
push @l, $line[$f++];;
my @exp = ();
while ($f < @line) {
if ( $line[$f] =~ m/^\[(\d+):(\d+)\]$/o ) {
# extract and store [n:n] on input or output lines
@exp=($1,$2);
} elsif ( $line[$f] =~ m/^\w+$/io) {
# expand "word" with [n:n] on input or output lines
push @l,join(", ",expand($line[$f],@exp));
} else {
push @l, $line[$f];
};
$f++;
};
} else {
# just append everything else to the output @l array
push @l, $line[$f];
};
$f++;
}
print join(" ",@l),"\n";
}
输出:
$ ./jigar.pl ./jigar.txt
module test ( temp_bus[3], temp_bus[2], temp_bus[1], temp_bus[0] , temp_B[1], temp_B[0] )
input temp_bus[3], temp_bus[2], temp_bus[1], temp_bus[0] ;
output temp_B[1], temp_B[0] ;
endmodule
第二个样本的输出:
$ ./jigar2.pl jigar2.txt
module test ( temp_bus[3], temp_bus[2], temp_bus[1], temp_bus[0] , temp_B[1], temp_B[0] , temp_C[1], temp_C[0] )
input temp_bus[3], temp_bus[2], temp_bus[1], temp_bus[0] ;
output temp_B[1], temp_B[0] , temp_c[1], temp_c[0] ;
endmodule
答案2
如果您只对示例中的间隔感兴趣,那么执行以下操作会很尴尬,但也不是太困难sed
:
/(in|out)put/s/(\[.*\]+) *(.*);/\2\1;/
s/([A-Za-z_]+)\[3:0\]/\1[3], \1[2:0]/g
s/([A-Za-z_]+)\[2:0\]/\1[2], \1[1:0]/g
s/([A-Za-z_]+)\[1:0\]/\1[1], \1[0]/g
一个稍微更通用的解决方案也涉及更多:
/(in|out)put/s/(\[.*\]+) *(.*);/\2\1;/
/\[[0-9:]+\]/s/$/#9876543210/
:a {
s/([A-Za-z_]+)\[([0-9]):0\](.*)(#[0-9]+)\2([0-9]+)$/\1[\2], \1[\5]\3\4\2\5/
ta
}
s/#9876543210$//
:b {
s/([A-Za-z_]+)\[([0-9])([0-9]+)\]/\1[\2], \1[\3]/
tb
}
我并不是建议这样做。