- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: parse a file and merge another file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-21-2010 06:03 AM
тАО04-21-2010 06:03 AM
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
Solved! Go to Solution.
- Tags:
- insert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-21-2010 06:35 AM
тАО04-21-2010 06:35 AM
Solution# 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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-21-2010 07:37 AM
тАО04-21-2010 07:37 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-21-2010 07:54 AM
тАО04-21-2010 07:54 AM
Re: parse a file and merge another file
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-22-2010 12:08 AM
тАО04-22-2010 12:08 AM
Re: parse a file and merge another file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-22-2010 04:24 AM
тАО04-22-2010 04:24 AM
Re: parse a file and merge another file
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-22-2010 05:07 AM
тАО04-22-2010 05:07 AM
Re: parse a file and merge another file
> 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