parse a file and merge another file (before a mark) (173 Views)
Reply
Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 1 of 3 (173 Views)
Accepted Solution

parse a file and merge another file (before a mark)

hello ,

 

i have a new issue due this thread

 

parse a file and merge another file

 

i want to insert another file before a fix defined mark and keep the mark.

 

in the other solution the marks will be replace with the variable insert file

 

START_TOKEN="TEST_MARK" ./filter.pl variable1 template

 

#!/usr/bin/perl -i

use strict;
use warnings;
my $start    = $ENV{START_TOKEN} || die "no START 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/ ) {
        /$start/ and print @contents; printf "\n$start\n";
        next;
    }
    print;
}
1;

 are the changes in regulatory ? a little wish will be , when a blank line before the "mark" exists , can this blank file maybe removed ?

 

regards

 

Acclaimed Contributor
Dennis Handly
Posts: 25,067
Registered: ‎03-06-2006
Message 2 of 3 (147 Views)

Re: parse a file and merge another file (before a mark)

>are the changes in regulatory?

 

Do you mean are they correct?  You could just try it.

 

>when a blank line before the "mark" exists, can this blank line be removed?

 

Something like this:

my $was_blank = 0;

while (<>) {

    if ( /^\s$/ ) {  # found blank line

       if ($was_blank) {

          printf "\n";  # multiple blank lines, print all but last

       }

       $was_blank = 1;       
        next;  # suppress
    }
    if ( /^$start/ ) {
        /$start/ and print @contents; printf "\n$start\n";
        next;
    }

    if ($was_blank) {

       printf "\n";  # print previous blank line

       $was_blank = 0;       
    }

    print;
}

Valued Contributor
support_billa
Posts: 192
Registered: ‎06-27-2011
Message 3 of 3 (135 Views)

Re: parse a file and merge another file (before a mark)

hello,

 

i tested your version and in same cases i have to insert a blank line. so i extended with my moderate skills your version :

 

START_TOKEN="TEST_MARK" ./filter.pl Y variable1 template   # Y ... YES and insert a blank line
START_TOKEN="TEST_MARK" ./filter.pl N variable1 template

use strict;
use warnings;

my $was_blank = 0;

my $start             = $ENV{START_TOKEN} || die "no START token defined\n";
my $insert_blank_line = shift or die "Usage $0: blank_line([Y|N]) varfile infile [n-infile]\n";
my $varfile           = shift or die "Usage $0: blank_line([Y|N]) varfile infile [n-infile]\n";

my $chr_line = ($insert_blank_line eq 'Y' ? "\n" : "");

open( my $fh, "<", $varfile ) or die "can't open '$varfile': $!\n";
my @contents = <$fh>;
close $fh;
while (<>) {
    if ( /^\s$/ ) {  # found blank line
       if ($was_blank) {
          printf "\n";  # multiple blank lines, print all but last

       }
       $was_blank = 1;       
       next;  # suppress
    }
    if ( /^$start/ ) {
        /$start/ and printf "$chr_line"; print @contents; printf "\n$start";
        next;
    }

    if ($was_blank) {
       printf "\n";  # print previous blank line
       $was_blank = 0;
    }
    print;
}
1;

 

regards

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.