基于mti_clk_chk关键字的双日志文件行匹配与差异对比需求
Alright, let's get this log comparison sorted out. Since you've already extracted all lines containing mti_clk_chk from both logs, we just need to add the logic to compare the relevant parts and fulfill your two requirements. Here's a straightforward Perl solution tailored to your needs:
Step-by-Step Explanation & Code
First, we'll use hash tables to efficiently track the content after mti_clk_chk: from each log file. This lets us quickly check for matches or missing entries.
use strict; use warnings; # Configure your extracted file paths here my $x_extracted = 'x_extracted.txt'; # Lines pulled from x.log with mti_clk_chk my $y_extracted = 'y_extracted.txt'; # Lines pulled from y.log with mti_clk_chk # Helper: Extract content after mti_clk_chk: (adjust trim if needed) sub get_mti_payload { my ($line) = @_; if ($line =~ /mti_clk_chk:(.*)$/) { # Uncomment the next line if you want to ignore leading/trailing whitespace differences # return trim($1); return $1; # Strictly match the exact content after the colon } return undef; } # Helper: Trim whitespace (optional) sub trim { my ($str) = @_; $str =~ s/^\s+//; $str =~ s/\s+$//; return $str; } # 1. Build a map of all mti_clk_chk payloads from y.log my %y_payloads; open my $y_fh, '<', $y_extracted or die "Failed to open $y_extracted: $!"; while (my $line = <$y_fh>) { chomp $line; my $payload = get_mti_payload($line); $y_payloads{$payload} = 1 if defined $payload; } close $y_fh; # 2. Find lines in x.log that don't match any in y.log print "--- Mismatched Lines in x.log (Payload Not Found in y.log) ---\n"; my %x_payloads; open my $x_fh, '<', $x_extracted or die "Failed to open $x_extracted: $!"; while (my $line = <$x_fh>) { chomp $line; my $payload = get_mti_payload($line); next unless defined $payload; $x_payloads{$payload} = 1; print "$line\n" unless exists $y_payloads{$payload}; } close $x_fh; # 3. Count lines in y.log that don't appear in x.log print "\n--- Count of Lines in y.log Missing from x.log ---\n"; my $missing_count = 0; open my $y_fh2, '<', $y_extracted or die "Failed to re-open $y_extracted: $!"; while (my $line = <$y_fh2>) { chomp $line; my $payload = get_mti_payload($line); next unless defined $payload; $missing_count++ unless exists $x_payloads{$payload}; } close $y_fh2; print "Total missing entries: $missing_count\n";
Key Notes:
- Strict vs. Lenient Matching: By default, the code matches the exact string after
mti_clk_chk:. If you want to ignore leading/trailing whitespace (like the extra space in your y.log example), uncomment thetrimline inget_mti_payload. - Single Extracted File: If all your extracted lines are in one
file.txt(with a marker like[x]or[y]at the start of each line), modify the file reading section to split entries into%x_payloadsand%y_payloadsbased on the marker. For example:# Example for combined file.txt with [x]/[y] markers open my $combined_fh, '<', 'file.txt' or die "Failed to open file.txt: $!"; while (my $line = <$combined_fh>) { chomp $line; if ($line =~ /^\[x\]\s*(.*)$/) { my $payload = get_mti_payload($1); $x_payloads{$payload} = 1 if defined $payload; } elsif ($line =~ /^\[y\]\s*(.*)$/) { my $payload = get_mti_payload($1); $y_payloads{$payload} = 1 if defined $payload; } } close $combined_fh;
This code will efficiently handle your two requirements: printing mismatched lines from x.log and counting missing lines from y.log.
内容的提问来源于stack exchange,提问作者Rahul Agarwal
相关产品推荐
相关产品推荐

