0

I have some files(.v.gz). The data is present in the file is shown below

syntax:

module **module_name**(out, In, clk, rst )
statement 1
statement 2
   :
   :
statement n
endmodule

the actual data is

file

module mod_reg_lif(out, In, clk, rst )  #
statement 1
statement 2
   :
   :
statement n
endmodule

module 
    dff_reg_net(out, In, clk, rst )  
statement 1
statement 2
   :
   :
statement n
endmodule

module 
   dat_log_out (out, In, clk, rst ) 
statement 1
statement 2
   :
   :
statement n
endmodule

module rest_wire_lib  (out, In, clk, rst ) 
statement 1
statement 2
   :
   :
statement n
endmodule

actual file contains similar type of multiple data

module switch(in1, datainready1, atainready) 
always @(posedge clk) 
begin 
  if (reset == 1)
  begin 
    counter<= 0;
      datainreadyl<= 0;
    if(counter ==0) 
      dataoutready<= 0;
   end 
endmodule

here switch is a module name

The above example please check the module_names are mod_reg_lif ,dat_log_out ,dff_reg_net, rest_wire_lib ,

consider after module as module_name (in the same line or next line it stars) till ( occurs if it's in the same line or next line. in output, I want only module names like this

mod_reg_lif   
dat_log_out 
dff_reg_net
rest_wire_lib  

what output I am getting

mod_reg_lif   
rest_wire_lib 

I am not able to fetch all the data as I mentioned in the output, because both module and module_name are present in the same line.

My bash code is

for file in *.v.gz;
do
  zgrep -A1 "^module" "$file" | sed -n -e 's/^\(module \)*\(.*(.*)\).*$/\2/p' | cut -f1 -d"(" > $(basename "$file" .v.gz).txt
done

please correct my sed -n -e 's/^(module )(.(.)).$/\2/p' command i am not sure how to fix.

ps: please comment if you have any doubts about the question.

13
  • Can you have parentheses ( ( ) ) anywhere else or will they always be just after the module name? Also, what operating system are you using? Can we assume GNU tools?
    – terdon
    Commented Nov 22, 2022 at 10:23
  • @terdon yes I am using parentheses for other operations inside the module(statements...). but this will eliminate in the grep step. I am using RedHat Unix Commented Nov 22, 2022 at 10:34
  • @terdon i am considering after module everything is module name till first open parentheses. this will repeat Commented Nov 22, 2022 at 10:36
  • Thanks. But please don't add comments to the file, the file should be exactly as your real data.
    – terdon
    Commented Nov 22, 2022 at 10:50
  • 1
    When you say after module everything is module name till first open parentheses - does that mean a module name can include white space? If so, again, edit your example to include such a case.
    – Ed Morton
    Commented Nov 22, 2022 at 11:05

2 Answers 2

2

This might be what you're trying to do, using GNU awk for multi-char RS, RT, \< word boundary, and \s shorthand for [[:space:]]:

$ zcat *.v.gz | awk -v RS='\\<module\\s+[^[:space:](]+' 'RT{$0=RT; print $2}'
mod_reg_lif
dff_reg_net
dat_log_out
rest_wire_lib
2
  • It's good if you added some explanation @EdMorton Commented Nov 23, 2022 at 4:07
  • We can't explain basic constructs in every answer - please look up whatever it is you don't understand in the man page and then let me know if you have any specific questions about anything and I'll be happy to explain.
    – Ed Morton
    Commented Nov 23, 2022 at 12:22
2

From the example you give, it looks like parentheses only occur just after the module name and module names have no whitespace. If so, you could do:

$ zgrep -oP '\S+\s*(?=\()' file.v.gz 
mod_reg_lif(
dff_reg_net
dat_log_out 
rest_wire_lib   

The extra ( in mod_reg_lif( is there because your example has module mod_reg_lif((out, In, clk, rst ), which I assume is a typo.

The idea here is to use PCRE mode in grep to get lookaheads which lets us look for non-whitespace characters (\S+) followed by 0 or more whitespace characters (\s*) and then a ( ((?=\()).

If you don't have a grep with a -P option, you can do:

$ zgrep -o '[^[:blank:]]*[[:blank:]]*(' file.v.gz | sed 's/ *(//g'
mod_reg_lif
dff_reg_net
dat_log_out
rest_wire_lib

If you cannot use the parentheses, if there are other places in your file where parentheses may occur, so you need to use the module string to identify module names, you can try:

$ zcat file.v.gz | perl -007ne 'print join("\n",(/\bmodule\s*(\S+?)\s*\(/gs)),"\n"; '
mod_reg_lif
dff_reg_net
dat_log_out
rest_wire_lib

Note that this will load the entire file into memory which might be an issue for huge files.


Finally, you can also use awk, setting the record separator to two the empty string which causes awk to work in "paragraph mode" where records (lines) are defined by two consecutive newline characters. Then, remove anything after the first parenthesis on a line and print the second field:

$ zcat file.v.gz | awk 'BEGIN{RS=""}{sub(/\(.*/,"");print $2; }'
mod_reg_lif
dff_reg_net
dat_log_out
rest_wire_lib
8
  • suppose you can not add module_name because in file almost 1000 module_name present @terdon Commented Nov 22, 2022 at 10:59
  • 1
    @SanthoshNayakD. sorry, I don't understand. What is module_name? That isn't mentioned in your question.
    – terdon
    Commented Nov 22, 2022 at 11:01
  • 1
    @SanthoshNayakD. yes, but why is that relevant? Both the second and the third solutions should work, right?
    – terdon
    Commented Nov 22, 2022 at 11:53
  • 1
    @SanthoshNayakD. this works as expected on your example. If your real data are different, please make that clear in your question. I don't know what "because of $ its gives an error" means, and if you have comments, again, then we would need to see that in the file and know what defines a comment.
    – terdon
    Commented Nov 22, 2022 at 12:06
  • 1
    Oooh! Yes, sorry @SanthoshNayakD. The $ indicates the shell prompt, it's the character you see when you are about to type something on the command line. When showing both a command and its output, it is common to add the $ in front of the command so you can visually separate the command from its output.
    – terdon
    Commented Nov 22, 2022 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .