如何编写Perl脚本比较两个多行文本标量?忽略空格及指定行
Perl Script to Compare Multi-line Strings (Ignoring Spaces & Specific Lines)
Got it, let's put together a Perl script that meets your requirements. The core idea is to process both input strings to strip out the parts we don't care about, then compare the cleaned-up content.
Here's the complete script:
#!/usr/bin/perl use strict; use warnings; # Your input multi-line strings my $string1 = <<'END_STRING'; logging on enable secret 5 $1$okZh$nSIVWr7vNHAqk231PisjN0 snmp-server community sph1nkx5 v1default RO 5 snmp-server ifindex persist tacacs-server timeout 5 tacacs-server key 7 xxx END_STRING my $string2 = <<'END_STRING'; logging on enable secret 5 xxx snmp-server community xxx snmp-server ifindex persist tacacs-server timeout 5 tacacs-server key 7 00070A160C5E190A002247 END_STRING # Helper function to clean up the input string sub clean_string { my ($input) = @_; my @clean_lines; # Split input into individual lines foreach my $line (split /\n/, $input) { # Skip lines starting with our target patterns (allow leading spaces) next if $line =~ /^\s*enable secret/; next if $line =~ /^\s*snmp-server community/; # Remove ALL spaces from the line $line =~ s/\s+//g; # Add non-empty cleaned lines to the result push @clean_lines, $line if $line ne ''; } # Return cleaned lines as a sorted string (sorted to avoid line order discrepancies) return join("\n", sort @clean_lines); } # Process both strings my $cleaned_str1 = clean_string($string1); my $cleaned_str2 = clean_string($string2); # Compare and output results if ($cleaned_str1 eq $cleaned_str2) { print "The processed strings are identical!\n"; } else { print "Differences found between processed strings:\n"; print "\n--- Cleaned String 1 ---\n$cleaned_str1\n"; print "\n--- Cleaned String 2 ---\n$cleaned_str2\n"; }
Let's break down how this works:
- Input Handling: We use Perl here-documents (
<<'END_STRING') to safely define multi-line strings without worrying about escaping special characters. - Cleanup Logic:
- Split the input into lines with
split /\n/to process each line individually. - Skip lines that start with
enable secretorsnmp-server community(the^\s*in the regex accounts for any leading spaces just in case). - Remove all spaces from remaining lines with
s/\s+//g—this ignores spaces anywhere in the line, not just leading/trailing. - Filter out empty lines (in case removing spaces leaves nothing) and sort the remaining lines. Sorting ensures line order doesn't break the comparison; if you need to preserve line order for comparison, just remove the
sortcall.
- Split the input into lines with
- Comparison: We check if the cleaned, sorted strings match. If not, we print both cleaned versions to highlight exactly where the differences lie.
Example Output for Your Input:
The script will detect the differing tacacs-server key lines and output:
Differences found between processed strings: --- Cleaned String 1 --- loggingon snmp-serverifindexpersist tacacs-serverkey7xxx tacacs-servertimeout5 --- Cleaned String 2 --- loggingon snmp-serverifindexpersist tacacs-serverkey700070A160C5E190A002247 tacacs-servertimeout5
内容的提问来源于stack exchange,提问作者Rudraprasad Pradhan
相关产品推荐
相关产品推荐

