It's a shame that you did not tell us what you already have tried, and what went wrong with your attempt. You are also missing some important information: how are the output files supposed to look? Should they contain the delimiter lines? Or should they contain everything, including module and endmodule?
Since the text blocks seem not to contain any blank lines, here is a very lazy way to solve the problem you are having.
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $file = 'soc.v'; open(my $fh, "<", $file) or die "Cannot open the file!"; my @data; $/ = ""; while (my $paragraph = <$fh>) { $paragraph =~ m/^\s*module\s*([A-Za-z]+)/; push @data, { filename => $1, content => $paragraph }; } #use Data::Dumper; print Dumper \@data; for my $outfile (@data) { open(my $out, ">", $outfile->{filename}) or die "Cannot open the f +ile!"; print $out $outfile->{content}; }
This solution uses the "paragraph mode" - this line: $/ = ""; makes sure that the iterating over the contents of the file is done not by line, as it usually is, but by whole paragraphs. If you uncomment the Data::Dumper line, you will see that the data from your input file is parsed into an array of hash references, in a way that gives you the information about the contents of the output file and its name.
Good luck with your problem.
- Luke
In reply to Re: how to split file with some pattern
by blindluke
in thread how to split file with some pattern
by herman4016
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |