Created
August 17, 2012 17:10
-
-
Save tomhennigan/3380711 to your computer and use it in GitHub Desktop.
matcher '(.*@.*)' '$1' < input_file_with_emails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
define('DEBUG', false); | |
class Logger | |
{ | |
private $fp; | |
public function __construct($fp) { | |
$this->fp = $fp; | |
} | |
public function debug($msg) { | |
if (DEBUG) $this->log("debug: $msg"); | |
} | |
public function error($msg) { | |
$this->log("error: $msg"); | |
} | |
public function warning($msg) { | |
$this->log("warning: $msg"); | |
} | |
public function log($msg) { | |
fputs($this->fp, trim($msg) . "\n"); | |
fflush($this->fp); | |
} | |
} | |
$fp = fopen('php://stderr', 'w'); | |
$log = new Logger($fp); | |
if (count($argv) !== 3) { | |
$log->log("usage: {$argv[0]} 'pattern' 'output'"); | |
exit; | |
} | |
$pattern = $argv[1]; | |
$output = $argv[2]; | |
$fp = fopen('php://stdin', 'r'); | |
if (!$fp) { | |
$log->error('unable top open stdin'); | |
exit; | |
} | |
while ($line = fgets($fp)) { | |
preg_match_all($pattern, $line, $matches, PREG_SET_ORDER); | |
foreach ($matches as $match) { | |
$log->debug('Found match: ' . print_r($match, true)); | |
$outfmt = $output; | |
$replkeys = array(); | |
$replvals = array(); | |
foreach ($match as $key => $val) { | |
$replkeys[] = "\$$key"; | |
$replvals[] = $val; | |
} | |
$log->debug('Replacing keys: ' . print_r($replkeys, true)); | |
$log->debug('Replacing vals: ' . print_r($replvals, true)); | |
echo str_replace($replkeys, $replvals, $outfmt) . "\n"; | |
} | |
} | |
fclose($fp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment