BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

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

SOLVED
Go to Solution
Valued Contributor

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

 

2 REPLIES
Acclaimed Contributor

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;
}

Highlighted
Valued Contributor

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