If you have anything more complex than that, you should use a proper Verilog parser: Verilog-Perl.

However, given your simple input, this script I created 13 years ago should do the trick. It also supports SystemVerilog classes, and it warns about some compiler directives:

=head1 NAME B<hdl_splitlib> - Split up a single Verilog library file into several +files =head1 SYNOPSIS hdl_splitlib file ... =head1 DESCRIPTION Split up a single Verilog library file into several files. Each outpu +t file will contain a single C<module> or C<class>. Input is a file (or files). Output is written to the current directory (several files). Some warning messages may be sent to STDOUT. Known limitations: - `endcelldefine or `undef compiler directives may follow endmodul +e - Those pesky `define macros!! - module/class definitions which are commented out Example: hdl_splitlib lib.v =cut use warnings FATAL => 'all'; use strict; my $directives_found = 0; my $out = 'temp.v'; my $fh; my $modname; open $fh, '>', $out; while (<>) { print $fh $_; if (/^\s*end(?:module|class)\b/) { close $fh; rename $out, "$modname.v"; open $fh, '>', $out; } elsif (/^\s*(?:module|class)\s+(\w+)/) { $modname = $1; } if (/`(define|celldefine|undef)/) { # ` $directives_found = 1; print; } } if ($directives_found) { print "\nWarning. The input file may contain problematic compiler +"; print " directives (define, undef, celldefine).\n\n"; } unless (-z $out) { print "\nWarning. All lines after the last 'endmodule' "; print "line in the input file have been deleted. These "; print "should have only been comments.\n\n"; } unlink $out;

In reply to Re: how to split file with some pattern (Verilog) by toolic
in thread how to split file with some pattern by herman4016

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.