Hello everyone,

I need to remove certain multi-line entries from files with a format like this:
[reports] yearlystore = quarterlystore = monthlystore = weeklystore = dailystore = [server1] logfilepath1 = F:\Logfiles\ZWire\Zwire4\split-0\ex*.log servername = Zwire4 logfilepassword = logfileusername = id = a104136320003954 [server2] servername = Zwire6 logfilepath1 = f:\logfiles\zwire\zwire6\split-0\ex*.log id = a104136346513171 logfilepassword = logfileusername = [server3] id = a104136351461256 logfilepath1 = f:\logfiles\zwire\zwire7\split-0\ex*.log logfilepassword = servername = zwire7
There are many types of
[header] item = item2 = item3 =
entries in these files. I am only concerned with the
[server1] logfilepath1 = F:\Logfiles\ZWire\Zwire4\split-0\ex*.log servername = Zwire4 logfilepassword = logfileusername = id = a104136320003954
type of entries. I need to remove everything from [serverX] to the next [serverX+1] (but not including it). I will be removing entries based on the contents of the servername = line.

Making this even more complex, I will have to re-number the [serverX] entries if one is removed, so that they remain in sequential order!

I've tossed around doing something like this:
while (<IN>) { if (/\[server(\d)\]/) { do { $throwaway = <IN>; } until { $throwaway = /\[server(\d)\]/ } +; } else { push @output, $_; } print OUT @output;
Of course, this doesn't help me when I'm only looking for a particular server, or the re-numbering.

Perhaps something more like this?
undef $/; $file = <IN>; # to slurp whole file at once $file =~ s/ # some complex regex //m;
All advice appreciated from the wise monks!

Cheers,
ibanix

$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

In reply to Searching for and removing multi-line file entries by ibanix

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.