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.
--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
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.