Skip to content

Instantly share code, notes, and snippets.

@akirad
Last active May 25, 2016 11:35
Show Gist options
  • Save akirad/49e50cfada784a658dc73cddc55a5f65 to your computer and use it in GitHub Desktop.
Save akirad/49e50cfada784a658dc73cddc55a5f65 to your computer and use it in GitHub Desktop.
A perl script which finds strings which are in the $checkStrFile($ARGV[0]) but not in the $targetFile($ARGV[1]).
use strict;
use warnings;
my $checkStrFile = $ARGV[0];
my $targetFile = $ARGV[1];
open(my $csFh, "< $checkStrFile") or die "Can't open $checkStrFile: $!";
open(my $tgtFh, "< $targetFile") or die "Can't open $targetFile: $!";
while (my $checkLine = <$csFh>) {
next if ($checkLine =~/^#/);
chomp($checkLine);
my $found = 0;
while (my $tgtLine = <$tgtFh>) {
next if ($tgtLine =~/^#/);
# I should't use regular expressions here.
my $checkLineQuoted = quotemeta($checkLine);
# If $tgtLine has a pattern of $checkLineQuoted.
if ($tgtLine =~/$checkLineQuoted/) {
$found = 1;
last;
}
}
# Perl keeps a position of $tgtFh, so it is necessary to seek it to the beginning of the file.
seek($tgtFh, 0, 0);
if ($found == 0) {
print "$checkLine\n";
}
}
close($csFh);
close($tgtFh);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment