The example input tcl file content:

# Bind ::d::autoBindmap $intfcName -------------so many other lines------------------------------ ############ Power information for vss ##################### #---------------------------- #pg_vss s100 #---------------------------- #Add pg_vss_dl interface set intfcName pg_vss 1.0 s 100 #Add Verilog port binding for vss ::d::autoBindmap $module pg_vss $intfcName { VSSGND vss_dl; } ############ Power information for vdd ##################### #---------------------------- #pg_vdd s100 #---------------------------- #Add pg_vdd_dl interface set intfcName pg_vdd 1.0 s 100 #Add Verilog port binding for vss ::d::autoBindmap $module pg_vdd $intfcName { VDD vss_dl; } -----------------------so many other lines---------------------------- +--- #write component ::d::writeLib $module

Purpose:

I have a array (which is generated from other part of code- not presented here) consisting of strings. The array content is changing for different files. If any of the array element is found in a particular line of file using pattern matching , I need to remove 11 lines after that and the line itself from a file.

Problem:

This change should be made in current file itself. So, I tried opening the file in read-write mode. But, I am seeing garbage being printed.

For example (see input tcl file above) :

I need to remove all the lines (in this case 11) till "}" after "Power information for $rpow " (for example $rpow is vss) is found, with the line itself being removed -where vss is an element of my remove_power_list array. This removal should happen for all the array elements.

Please if anybody can solve this issue?

Only problematic code being presented here:

my $rpow; for $rpow (@remove_power_list) { my $bf_content; open $bf_content, '+<', $bf_bind; my $count = 0; while (<$bf_content>){ if($_ =~ /Power\s+information\s+for\s+$rpow\s+/) { $count = $. + 11; } else { if($count != 0 && $. < $count) { } elsif($count != 0 && $. == $count) { $count =0; } else { print $bf_content $_; } } } close $bf_content; }

Note: My perl packages does not support File:Slurp and other special Array packages.

Expected example output:

# Bind ::d::autoBindmap $intfcName -------------so many other lines------------------------------ ############ Power information for vdd ##################### #---------------------------- #pg_vdd s100 #---------------------------- #Add pg_vdd_dl interface set intfcName pg_vdd 1.0 s 100 #Add Verilog port binding for vss ::d::autoBindmap $module pg_vdd $intfcName { VDD vss_dl; } -----------------------so many other lines---------------------------- +---

In reply to Remove multiple lines in file if pattern found based on array element, in perl by MatD

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.