in reply to Searching for and removing multi-line file entries

This should work for you:
use strict; my %bad = ("Zwire6" => 1, "zwire7" => 1);#this is case sensitive open(OUT, ">", "out.txt"); my @section; my $adjust = 0; my $is_server = 0; my $server_id; my $throw_away = 0; my $line; while ($line = <DATA>) { chomp($line); if ($line =~ m/^\[/) { flush_section(); $#section = -1; $throw_away = 0; push @section, $line; if ($line =~ m/^\[server(\d+)\]/) { $server_id = $1; $is_server = 1; } else { $is_server = 0; } } else { push @section, $line; if ($is_server) { if ($line =~ m/^servername\s*=\s*(.*?)\s*$/) { if (defined($bad{$1})) { $throw_away = 1; $adjust ++; } } } } } flush_section(); close(OUT); sub flush_section { if (!$throw_away) { if ($is_server) { $section[0] = "[server".($server_id - $adjust)."]"; } foreach my $line (@section) { print OUT $line, "\n"; } } } __DATA__ [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] logfilepath1 = F:\Logfiles\ZWire\Zwire4\split-0\ex*.log servername = Zwire4 logfilepassword = logfileusername = id = a104136320003954 [server4] id = a104136351461256 logfilepath1 = f:\logfiles\zwire\zwire7\split-0\ex*.log logfilepassword = servername = zwire7

Replies are listed 'Best First'.
Re: Re: Searching for and removing multi-line file entries
by ibanix (Hermit) on Jan 03, 2003 at 02:51 UTC
    Thanks pg!

    In the time you must have been writing this, I developed a solution. I discovered that the sequential limitation was avoidable; that is: "serverX+1" did not have to follow "serverX".

    So I wrote this:
    use strict; use warnings; # use re "debug"; my $file = shift @ARGV || die "No file name given!\n"; my $server = shift @ARGV; my $output = shift @ARGV; open(IN, "$file") or die "Unable to open file\n"; undef $/; my $content = <IN>; $content =~ s/ ^\[server(\d)\] .*? servername\s=\s$server .*? \[server +\d\]$/\[server$1\]/msxi close(IN); open(OUT, ">$output"); print OUT $content; close(OUT);
    This only removes one server entry, but it will be easy enough to throw a foreach loop around it.

    Thanks again for the help!

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