Hey lotr,
I had some free time and wrote it in Perl for fun and for practice.
It’s a trifle crude, because I haven’t been practicing my Camel riding lately.
But it runs in sub-1-second times on my Mid-2010 i7 MacBook Pro.
I’ve tested it only on OSX 10.9.5 and run it from both BBEdit and FastScripts.
It is best if you continue to use a sub-folder to contain only those files operated on, as the script zeros in on the front Finder folder.
The script might or might not run on Snow Leopard. If it does NOT then please determine what version of Perl you have and let me know.
In the Terminal run:
perl --version
I’d also want to see any error messages generated.
The script should just run from BBEdit or TextWrangler.
If you run it from FastScripts or another script-runner you might have to make it executable.
The script still creates a ‘A-Filtered.txt’ file in the same folder as the source files, but it no longer creates a consolidated data-file.
It does specifically target files A.txt, B.txt, C.txt, & D.txt though.
#! /usr/bin/perl
use strict; use warnings;
use File::Slurp;
#--------------------------------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2015/07/12 18:00
# dMod: 2015/07/12 21:37
# Appl: Perl & App to run Perl
# Task: Filter lines out of control file if ip-address is not found in data-file-set.
# Tags: @Perl, @Script, @Filter, @IP-Address
# Vers: 1.00
#--------------------------------------------------------------------------------------
# Notes:
#
# Written for user lotr on MacScripter.net
#--------------------------------------------------------------------------------------
my ($applescript, $controlFile, $dataFileText, $fh, $myDir, $outputFilePath, $regExStr, @controlFileData, @newFileData);
$applescript = 'osascript -e "
tell application \"Finder\" to set frontFolderAlias to insertion location as alias
return POSIX path of frontFolderAlias
"';
$myDir = `$applescript`;
chomp $myDir;
$outputFilePath = $myDir.'A-Filtered.txt';
$controlFile = read_file( $myDir."A.txt" ) ;
$controlFile =~ s!(?m)^[[:blank:]]*\n!!g;
$dataFileText = read_file( $myDir."B.txt" ) . "\n";
$dataFileText = $dataFileText . read_file( $myDir."C.txt" ) . "\n";
$dataFileText = $dataFileText . read_file( $myDir."D.txt" ) . "\n";
$dataFileText =~ s!(?m)^[[:blank:]]*\n!!g;
@controlFileData = split("\n", $controlFile);
foreach (@controlFileData) {
if ( m!^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b! ) {
$regExStr = quotemeta($&);
$regExStr = '(?m)^'.$regExStr.'\b';
if ( $dataFileText =~ m!$regExStr! ) {
push(@newFileData, $_);
}
}
}
open($fh, '>', $outputFilePath) or die "Could not open file '$outputFilePath' $!";
$, = "\n";
print $fh @newFileData;
close $fh;