Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Go to solution
support_billa
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
Dennis Handly
Acclaimed Contributor
Solution

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
support_billa
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