1

Suppose I have a code mentioned below.

module dut#(parameter type tp =int, tp x = 12 ) (int r , reg [7:0] rg);
endmodule

module mid (int r, reg [7:0] rg); endmodule

module bin (int z, logix s); endmodule

module med; endmodule

I want to extract the words dut, mid, bin and med, characterized by being the words after the keyword module and before the symbol #, ( or ;, whichever comes first.

I want to accomplish this using only a csh script. Which regex can be used for that purpose?

AdminBee
  • 22,803
Aakash
  • 31

4 Answers4

2

Using (gnu)grep:

 grep -Po 'module +\K\w+' file
JJoao
  • 12,170
  • 1
  • 23
  • 45
2

Another solution using sed:

$ sed -nE 's/^module +([^ (#;]+) *[#(;].*$/\1/p' filename
dut
mid
bin

This will extract the module name by replacing (s) the entire line with the expression found in the parentheses.

Currently,

  • it looks for lines that start with "module" (^module), followed by one or more spaces ( +), and then followed by a string of one or more characters that are not space, ( or #, ;. This string is placed in a "capture group" because its specification [^ (#;]+ is placed inside parentheses ( ... ). The regular expression then forces zero or more spaces ( *), then either a #, a ( or a ; ([#(;]), and then any number of any character up to the end of the line (.*$), for a line to be considered a match.
  • If a match is found, the replacement is printed (p), but the -n option ensures that lines without match are not printed by default.

If you want to learn more about regular expressions, take a look here e.g.

AdminBee
  • 22,803
0

I'm not sure what you mean by saying only using csh script. Can you use standard programs? If yes then:

The simplest solution for me is using grep and awk.

grep "module \w*" -o filename | awk '{print $2}'
dut
mid
bin
0

with awk:

$ awk -F'[ \t#(;]+' '/^module/{ print $2}' infile
dut
mid
bin
med

print second field where separators are one of Space /Tab\t/#/; or ( characters and ignore repetition (+)

αғsнιη
  • 41,407