Skip to content

Instantly share code, notes, and snippets.

@azat
Last active December 17, 2015 06:28
Show Gist options
  • Save azat/5565095 to your computer and use it in GitHub Desktop.
Save azat/5565095 to your computer and use it in GitHub Desktop.
Grep blocks. (blocks are splitted by double CRLF)
#!/usr/bin/env php
<?
/**
* From https://github.com/azat/scripts/blob/master/block_grep.php
*/
if ($argc < 3) {
fprintf(STDERR, "Usage: %s [-v | --invert-match] pattern file-name\n",
$argv[0]);
exit(1);
}
$options = array();
$options = array_merge($options, getopt(
'v',
array(
'invert-match',
)
));
// TODO: add helper.
if (array_key_exists('v', $options)) {
// TODO: false is because PHP parse options in that way.
$options['invert-match'] = false;
}
$options['pattern'] = array_pop($argv);
$options['file'] = array_pop($argv);
preg_match_all('@(.*)[\n]{2,}@Uis', file_get_contents($options['file']),
$matches, PREG_SET_ORDER);
$matchedBlocks = 0;
foreach ($matches as $match) {
$content = $match[0];
$matched = preg_match($options['pattern'], $content);
// TODO: add helper.
if (array_key_exists('invert-match', $options)) {
$matched = !$matched;
}
if ($matched) {
$matchedBlocks++;
echo $content;
}
}
fprintf(STDERR, "Examined %d blocks. Matched %d blocks.\n", count($matches), $matchedBlocks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment