This is the contents of the file, of which I want to insert multiple lines after </module-data> if the process-type=Remote. Could you please help !!

--------------------------FILE------------------------------------------
<process-type="Local">
               <module-data>
               </module-data>
<process-type="Remote">
               <module-data>
               </module-data>
--------------------------FILE------------------------------------------

Recommended Answers

All 4 Replies

This works for me in a Linux environment:

perl -n -i.bak -e '$r=1 if m/<process-type="Remote">/;$m=1 if $r && m/<\/module-data>/;print;if ($r and $m){print "blah\n" x 7;($r,$m)=(0,0);}' file.txt

file.txt now contains:

--------------------------FILE------------------------------------------
<process-type="Local">
               <module-data>
               </module-data>
<process-type="Remote">
               <module-data>
               </module-data>
blah
blah
blah
blah
blah
blah
blah
--------------------------FILE------------------------------------------

Thanks a lot. Would appreciate if you could help me understand the conditions.

This works for me in a Linux environment:

perl -n -i.bak -e '$r=1 if m/<process-type="Remote">/;$m=1 if $r && m/<\/module-data>/;print;if ($r and $m){print "blah\n" x 7;($r,$m)=(0,0);}' file.txt

file.txt now contains:

--------------------------FILE------------------------------------------
<process-type="Local">
               <module-data>
               </module-data>
<process-type="Remote">
               <module-data>
               </module-data>
blah
blah
blah
blah
blah
blah
blah
--------------------------FILE------------------------------------------

This one liner is equivalent to

my ($r, @output);
open (my $FH, '<', 'file.txt')  || die "cannot open file.txt for input - $!";

#Add text after <\/module-data> (second condition)
#but only if it came after <process-type="Remote"> (first condition)

while (<$FH>) { #each line is assigned to $_
    chomp;
    if (m/<process-type="Remote">/) { #this is equivalent to if ($_ =~ m/<process...>/)
        $r = 1;
    }
    if ($r && m/<\/module-data>/) { #both conditions met
        $m = 1;
    }
    push @output, $_;
    if ($r and $m) { 
        push @output, "blah\n" x 7;
        ($r,$m)=(0,0); #reset flags
    }
}
close $FH;
open ($FH, '>', 'file.txt') || die "cannot open file.txt for output - $!";
print $FH join "\n", @output;
close $FH;

You can even give up the second flag

while (<$FH>) { #each line is assigned to $_
    chomp;
    push @output, $_;
    if (m/<process-type="Remote">/) { #this is equivalent to if ($_ =~ m/<process...>/)
        $r = 1;
    }
    if ($r && m/<\/module-data>/) { #both conditions met 
        push @output, "blah\n" x 7;
        $r = 0; #reset flag
    }
}

Or as the one-liner

perl -n -i.bak -e 'print; $r=1 if m/<process-type="Remote">/; if ($r && m{</module-data>}){print "blah\n" x 7;$r =0;}' file.txt
commented: Plus rien à retrancher :) +2

Thanks a lot..... You guys are awesome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.