954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Perl One-liner Help !!

This is the contents of the file, of which I want to insert multiple lines after 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------------------------------------------
rmsagar
Newbie Poster
18 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

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------------------------------------------
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
 

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------------------------------------------
rmsagar
Newbie Poster
18 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

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
erezschatz
Newbie Poster
18 posts since Jan 2011
Reputation Points: 26
Solved Threads: 4
 

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

rmsagar
Newbie Poster
18 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You