Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split fil into smaller files for each text-based subsection

Status
Not open for further replies.

czarj

Technical User
Apr 22, 2004
130
US
I have a large text file that contains multiple subsection. Each section starts and ends with the same text:

Starts with: %%%BEGIN_SECTION
Ends with: %%%END_SECTION%%%

I would like to break up each section into its own file and name the file by the section header. For example if my data file (myfile.txt) is comprised of the following data I would like to create three different files named "registry," "hostname" and "os_version."

Data File:
------------------------------myfile.txt------------------------------
%%%BEGIN_SECTION REGISTRY%%%
/usr/snapvault

%%%END_SECTION%%%

%%%BEGIN_SECTION HOSTNAME%%%
wuxsplm01

%%%END_SECTION%%%

%%%BEGIN_SECTION OS_VERSION%%%
SunOS wuxsplm01 5.10 Generic_141414-07 sun4u sparc SUNW,Sun-Fire-480R
testline1
2232
%%%END_SECTION%%%
----------------------------------------------------------------------

Resulting Files:
------------------------------registry------------------------------
/usr/snapvault
--------------------------------------------------------------------

------------------------------hostname------------------------------
wuxsplm01
--------------------------------------------------------------------

------------------------------OS version-----------------------------
SunOS wuxsplm01 5.10 Generic_141414-07 sun4u sparc SUNW,Sun-Fire-480R
testline
2232
--------------------------------------------------------------------



Currently I am doing this, which is working. However since I am not an efficient programmer this seems to be overly complicated and am looking for a more elegant solution.

Code:
#!/usr/bin/perl
$found = 0;
$filename = $ARGV[0];
open(OSSVINFO, "<$filename") || die "unable to open $filename: $!\n";

$hostname = "";

while(<OSSVINFO>) {

        chomp;
//;     $_ =~ s/
        if (/^Hostname:/) {
                $_ =~ s/^Hostname: (.+)$/$1/;
                $hostname = $_;
                break;
        }
}
close(OSSVINFO);

open(OSSVINFO, "<$filename") || die "unable to open $filename: $!\n";
while(<OSSVINFO>) {
        chomp;
//;     $_ =~ s/

        if(/BEGIN_SECTION/) {
                $_ =~ s/^%%%BEGIN_SECTION (.+)%%%/$1/;
                $file = $_;
                $filename = $hostname."_".$file;
                open(FILE, ">$filename");
                $found = 1;
        }

        if(/END_SECTION/) {
                $found = 0;
                close(FILE);
        }

        if ($found eq 1) {
                print FILE "$_\n";
        }
}
close(FILE);

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Hi

Hmm... If skipping empty lines in your code is intentional :
Code:
perl -ne 'close F if/END_SECTION/;print F[highlight] if/\S/[/highlight];open F,">".lc$1if/BEGIN_SECTION (\w+)/' myfile.txt

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top