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 = ) { 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