I have a file that is in the following format:
Preamble
---------------------
Section 1
...
---------------------
---------------------
Section 2
...
---------------------
---------------------
Section 3
...
---------------------
Afterwords
And I want to extract each section by the separator so that I’ll have a result in:
file0:
Section 1
...
file1:
Section 2
...
file2:
Section 3
...
…
Is there a simple way to do this? Thanks.
[Update] Using chomp and
$_makes this even shorter.This should do it:
If your input record separator is a sequence of 21
-‘s, this is easy withperl -ne:should work, and create files
file0..fileN.Explanation
Easier to explain as a stand-alone Perl-script perhaps?
Perl’s
awklineage was useful here, so let’s show anawkversion for comparion:Not too bad compared to the
perlversion, it’s actually shorter. The$/in Perl is theRSvariable inawk. Awk has an upper hand here:RSmay be a regular expression!