Operating System - HP-UX
1748136 Members
3837 Online
108758 Solutions
New Discussion юеВ

Re: parse a file and merge another file

 
SOLVED
Go to solution
Billa-User
Regular Advisor

parse a file and merge another file

hello,

following task i have to do:
search a file ( i call it "template"-file) and at fixed defined area it should include another file (i call it "variable"-file)

example:
"template"-file :

.
.
#BEGIN REPLACEMENT VARIABLE-FILE
#END REPLACEMENT VARIABLE-FILE
.
.
"variable"-file:
SETING1="..."

I want to replace the area between BEGIN and END with the content of "variable"-file.

explanation ok ?

preference : awk or perl,then shell

regards
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: parse a file and merge another file

Hi:

# cat ./filter
#!/usr/bin/perl
use strict;
use warnings;
my $varfile = shift or die "Usage $0: varfile infile\n";
open( my $fh, "<", $varfile ) or die "can't open '$varfile': $!\n";
my @contents = <$fh>;
while (<>) {
if ( /BEGIN/ .. /END/ ) {
/BEGIN/ and print @contents;
next;
}
print;
}
1;


...run as:

# ./filter varfile infile

Regards!

...JRF...
Billa-User
Regular Advisor

Re: parse a file and merge another file


hello,

great and my last request:
the "keywords" BEGIN and END should be also variable, so

# ./filter varfile infile BEGIN END

thank you,tom
James R. Ferguson
Acclaimed Contributor

Re: parse a file and merge another file

Hi:

> he "keywords" BEGIN and END should be also variable

OKl use:

# cat ./filter
#!/usr/bin/perl
use strict;
use warnings;
my $start = $ENV{START_TOKEN} || die "no START token defined\n";
my $stop = $ENV{STOP_TOKEN} || die "no STOP token defined\n";
my $varfile = shift or die "Usage $0: varfile infile\n";
open ( my $fh, "<", $varfile ) or die "can't open '$varfile': $!\n";
my @contents = <$fh>;
while (<>) {
if ( /$start/../$stop/ ) {
/$start/ and print @contents;
next;
}
print;
}
1;

...now run like this:

# START_TOKEN=BEGIN STOP_TOKEN=END ./filter varfile infile

Notice that there are *no* semicolons in the above commandline. This obviates the need to export the START_TOKEN and STOP_TOKEN into your environment.

You could process multiple files by doing:

# START_TOKEN=BEGIN STOP_TOKEN=END ./filter varfile infile1 infile2 infile3 ...

Regards!

...JRF...

Billa-User
Regular Advisor

Re: parse a file and merge another file

great , maybe you have for my task also a solution , better will be :

START_TOKEN=BEGIN_VARIABEL1 STOP_TOKEN=END_VARIABLE1 ./filter varfile infile1
START_TOKEN=BEGIN_VARIABEL2 STOP_TOKEN=END_VARIABLE2 ./filter varfile infile2

i make a shell script named "filter.sh" and work with temporary files.

i append examples in a GZ File (Format TAR (uncompress gzip -d file and extract tar -xvf file)


i my example the output are digits from 1 til 12

regards,tom
James R. Ferguson
Acclaimed Contributor

Re: parse a file and merge another file

Hi (again) Tom:

> great , maybe you have for my task also a solution

I do, if I understand correctly what you want: i.e. the elimination of temporary files.

Hence:

# cat ./filter.pl
#!/usr/bin/perl -i
use strict;
use warnings;
my $start = $ENV{START_TOKEN} || die "no START token defined\n";
my $stop = $ENV{STOP_TOKEN} || die "no STOP token defined\n";
my $varfile = shift or die "Usage $0: varfile infile\n";
open( my $fh, "<", $varfile ) or die "can't open '$varfile': $!\n";
my @contents = <$fh>;
close $fh;
while (<>) {
if ( /$start/ .. /$stop/ ) {
/$start/ and print @contents;
next;
}
print;
}
1;

# cat ./filter.sh
parse_file()
{

cp -p template template.old #...preserve the original file...

START_PARSE="# BEGIN-PARSE-DATAFILE-DEFINITION #"
END_PARSE="# END-PARSE-DATAFILE-DEFINITION #"
START_TOKEN="${START_PARSE}" STOP_TOKEN="${END_PARSE}" ./filter.pl variable1 template

START_PARSE="-- BEGIN-PARSE-DATABASE-DEFINITION -"
END_PARSE="-- END-PARSE-DATABASE-DEFINITION -"

START_TOKEN="${START_PARSE}" STOP_TOKEN="${END_PARSE}" ./filter.pl variable2 template

START_PARSE="-- BEGIN-PARSE-TABLESPACE-DEFINITION -"
END_PARSE="-- END-PARSE-TABLESPACE-DEFINITION -"

START_TOKEN="${START_PARSE}" STOP_TOKEN="${END_PARSE}" ./filter.pl variable3 template

}

parse_file
exit

...Notice that a backup of the original file at the beginning of your shell wrapper occurs. Then, in-place modification occurs, leaving a modified file and a backup copy without the clutter of temporary files to cleanup or correctly enumerate in a script.

Since only one attachment at a time can be made, I have pasted the two scripts, inline, above. Your other files in the 'gzip' archive you posted are unchanged.

Regards!

...JRF...
Billa-User
Regular Advisor

Re: parse a file and merge another file

great, great. It's a peach.

> I do, if I understand correctly what you want: i.e. the elimination of temporary files.

yes, you are right. your solution solve my task. background for my script is :

i have one template-file and replace the TOKEN with several variable files ( in those files are different settings)

thank you very much

regards,tom