Last active
December 11, 2015 14:38
-
-
Save kulp/4615404 to your computer and use it in GitHub Desktop.
Filter stdin to stdout, using line-ranges described by binary strings.
This file contains hidden or 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 perl | |
# Takes M parameters on the command line, binary strings of N digits. | |
# Filters stdin to stdout, printing only lines whose indices fall inside (or | |
# outside, if -invert is specified) the independent ranges specified by the | |
# binary string, where "" means "everything", "0" means "the first half", "01" | |
# means "the first quarter", "111" means "the last eighth" and so on. | |
use strict; | |
my @lines = <STDIN>; | |
my $count = @lines; | |
for my $spec (@ARGV) { | |
my $val = oct "0b$spec"; | |
my $width = 1 << (length $spec); | |
my $start = $val / $width; | |
my $end = $start + (1 / $width); | |
my ($a, $b) = ($start * $count, $end * $count - 1); | |
if ($::invert) { | |
@lines[$a .. $b] = undef; | |
} else { | |
print @lines[$a .. $b]; | |
} | |
} | |
print grep defined, @lines if $::invert; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment