0

I have empty modules in my Verilog file (written automatically) which I want to remove upon writing the file.
e.g.

module noneed ;  
endmodule

My plan is to find these lines and remove them from my file, so I came up with this (non-working) pipeline:

grep -P -A 1 -n 'module \S+ \;' myfile.v \
  | perl -p -e 's/(\d+).*/$1/' \
  | grep -v '-' \
  | xargs -0 -i sed '{}d' myfile.v

My line: find empty modules, take their line numbers (and the line after) and feed it to sed for removing.

What have I done wrong here and how can I fix my one-liner to actually remove the lines?

BTW, xargs is cool and everything, but I don't care for a solution not using it.

Edit
here's a test text:

buf syn_1 ( .a ( clkin ) , .vcc ( vcc ) ,  .vss ( vss ) ) ;  
endmodule  
module some_bu11shit_module_nobody_needs_234 ;  
endmodule  
module a_real_module ( clk , inputs , vcc , vss ) ;  
input  clk ;  

EDIT2
desired output:

buf syn_1 ( .a ( clkin ) , .vcc ( vcc ) ,  .vss ( vss ) ) ;  
endmodule  
module a_real_module ( clk , inputs , vcc , vss ) ;  
input  clk ;  
user2141046
  • 862
  • 2
  • 7
  • 21
  • 2
    It seems you already use Perl. Why not to use [Verilog::Parser](http://p3rl.org/Verilog::Parser)? – choroba Jan 23 '18 at 10:23
  • never used it (or heard of it till now). in any case, i'm aiming to a one-liner and i don't know how to use CPANs in oneliners – user2141046 Jan 23 '18 at 10:33
  • 1
    Possible duplicate of [sed or awk: delete n lines following a pattern](https://stackoverflow.com/questions/4396974/sed-or-awk-delete-n-lines-following-a-pattern) – Sundeep Jan 23 '18 at 10:57
  • see also https://stackoverflow.com/questions/17908555/printing-with-sed-or-awk-a-line-following-a-matching-pattern – Sundeep Jan 23 '18 at 10:58
  • @Sundeep - thanks for the links, but sed doesn't support PCRE regex syntax, so I really can't give it a pattern that describes things of the sort of `a_one234five_cell_name` – user2141046 Jan 23 '18 at 11:37
  • 1
    GNU sed supports `\S` and all POSIX tools support `[^[:space:]]` .. and you can use `[a-zA-Z0-9_]` if those are the valid characters allowed – Sundeep Jan 23 '18 at 11:38
  • I'm in a tcsh (should be GNU), in any case, neither `sed -e '/module \S+ \;/,+1d'` or `'/module [^[:space:]] \;/,+1d'` works. Thanks for your effort, btw... – user2141046 Jan 23 '18 at 11:42
  • Can you show the desired output corresponding to your test input? That would help clarify the question. As it is, I don't see why a simple `sed -i -e '/^module noneed/,/endmodule/' *.v` isn't sufficient. – Toby Speight Jan 23 '18 at 11:53
  • @Sundeep - getting `sed: invalid option -- 'E'` when using -E @TobySpeight - edited the question to have the desired output as well – user2141046 Jan 23 '18 at 12:13
  • got it! thank you all for the help – user2141046 Jan 23 '18 at 12:15

2 Answers2

1

sed -e '/\bmodule[^(]*$/,/endmodule/d' myfile.v

For any line with word boundary + module and no open parens, delete until endmodule.

Effectively removes modules with no pins, for typical ways of formatting Verilog.

A robust solution would instead use semicolons as line delimiters.

That said, this is /START/,/END/{ ... } syntax we use with sed quite a lot editing netlists:

sed \
    -e '/\.SUBCKT DELETEME\b/,/\.ENDS/d' \
    -e '/module DELETETHISTOO\b/,/endmodule/d' \
    input-file > output-file

... because even though spice can have line continuations and verilog can have arbitrary line breaks until a semicolon, most netlisting tools can be told to avoid those more difficult cases.

stevesliva
  • 5,351
  • 1
  • 16
  • 39
  • your answer seems valid, but it doesn't do the trick. ls -l myfile.v ... 65363912 ... myfile.v sed -e '/module[^(]*$/,/endmodule/d' myfile.v ls -l myfile.v ... 65363912 ... myfile.v – user2141046 Jan 24 '18 at 09:05
  • Was working worse for me with some broadened testcases. Need either a line start anchor (`^`) or a word boundary (`\b`) before `module` or it matches every endmodule line. Hopefully works for you now. – stevesliva Jan 24 '18 at 14:28
0

with the help of @Sundeep, I came up with this line that does it super quick.

sed -i -re '/module \S+ ;/,+1d' myfile.v

Thanks

user2141046
  • 862
  • 2
  • 7
  • 21