App-expskip
view release on metacpan or search on metacpan
#!/usr/bin/perl
# expskip
# Producec by Toshiyuki Shimono, Tokyo., 2016-01 ~ 07 , 2018-3, 2022-10
# æåã¯ããã¡ã¤ã«ã®å
é ã¨æå¾ã®3è¡ã®ã¿ãåºãã¦ãããã
# éä¸ãææ°é¢æ°çãªè¡çªå·ãåºãããã«ãã¦ã¿ãã
# ãã¡ã¤ã«å㯠headtail, pickall, expskip ã¨å¤é·ãã¦ããã
# 使è
: ä¸éå¯¿ä¹ bin4tsv@gmail.com
use 5.014 ; use warnings ;
use Getopt::Std ;
use Term::ANSIColor qw[ color :constants ] ; $Term::ANSIColor::AUTORESET = 1 ;
use FindBin qw [ $Script ] ;
# ãã¹ã/ã㢠test/demo
if ( exists $ARGV[0] && $ARGV[0] =~ m/^-T/ ) {
my $A0 = 1 ;
my $arg = do { $ARGV[0] =~ m/^-T(.+)$/ && $1 || undef } // do { $A0 ++ ; $ARGV[1] } ; # -T ã®ãã©ã¡ã¼ã¿ãæ½åºã
do { say STDERR RED "Specify the parameter for `-T' such as `$0 -T 1m'." ; exit } if ! defined $arg ;
my $cmd = '' ;
do {
no warnings 'experimental::smartmatch' ;
$cmd = "seq 20 | $0" when '20' ; # -T 20 ã§ "expskip" ã« seq 20 ã®çµæãä¸ããã
$cmd = "seq 20 | $0 -f5" when 'f5' ; # -T f5 ã§ "expskip -f5" ãã·ãã¥ã¬ã¼ã
$cmd = "seq 20 | $0 -p0" when 'p0' ; # -T p0 ã§ "expskip -p0" ãã·ãã¥ã¬ã¼ã
$cmd = "$0 <(seq 4) <(seq 10)" when 'm2' ; # -T m2 ã§ãè¤æ°ã®ãã¡ã¤ã«ãä¸ããã
$cmd = "seq 1e6 | $0" when '1m' ; # -T 1m ã§ "expskip" ã« 100ä¸ã¾ã§ã®æ°ãä¸ããã
$cmd = "seq 1e5 | $0 -e0" when 'e0' ; # -T e0
$cmd = "seq 1e5 | $0 -f5" when 'f5' ; # -T f5
$cmd = "seq 100 | tr 0-9 A-H | $0 -: 'yellow green bold italic'" ; # -T ac ã§ãæ°å以å¤ã¨è¡çªå·ã®è²ã¥ããåæã«ãã¹ãã
$cmd = "seq 1e7 | $0" when 'E7' ; # -T E7 ã§ 1000ä¸ã¾ã§ã®æ°ãä¸ããããªããseq 1e7ã1e7+1ã®æ°ãä¸ããã
} for $arg ;
$cmd = join " " , $cmd , @ARGV [ $A0 .. $#ARGV ] ; # <-- @ARGV ã«'ã"ãããã¨æã£ãããã«åä½ããªãã§ãããã
say STDERR YELLOW BOLD "> $cmd" ;
system $cmd ;
exit ;
}
getopts "::2:b:e:f:tp:zA:B:T:",\my%o ; # <-- -T以å¤ã®ãªãã·ã§ã³ãèªã¿åãã(ãã ãå使¤è¨¼ã®ãããTã¯ããã«æ®ããã)
eval "use PerlIO::gzip;1" or die "Can't import PerlIO::gzip despite -z instruction. ($Script)\n" if $o{z} ;
sub lineOut ( $ ) ; # lineOutã®ãããªé¢æ°åããã¤ãã®ãã¡ãã©ããã使ãããã
sub eachFile ( $ ) ;
sub fitCheck ( $ ) ;
$| = 1 ; # <- æ¯åãã©ãã·ã¥(使ãããç¶æ³ãèããã¨å¤åããã§è¯ãã¨æãããã)
$o{b} //= 10 ; # åºæ°ã®æå®ãbase
$o{e} //= 2 ; # æåã¨æå¾ã®ããããä½è¡ãåºåããããEdge
$o{f} ||= 1 ; # éå§è¡ãæå®ããã0ã§ãundefã§ãå¤ã1ã«ãªãããã«ãããfrom
$o{A} //= 0 ; # åè´ããè¡ã®ä½è¡å¾ã¾ã§ããã«ç¶ãã¦åºåããã After
$o{B} //= 0 ; # åè´ããè¡ã®ä½è¡åã«ããã®ã¼ã£ã¦ç¶ãã¦åºåããã Before
my (@nums0, @nums) ; # ä½è¡ç®ã-bã¨-pã®æ©è½ã§åºåãããã«ã¤ãã¦ã®è¡çªå·ãä¿ç®¡ã0ã¯ãã¹ã¿ã¼ãæå³ããã
my $oc ; # åãã¡ã¤ã«ã§åºåããè¡ãæ°ããã
# (åºåãã)æ¸å¼ã®æå® : å颿°ã¯ãã©ããã1åã ãã®å¼æ°ã ãããããç¡åé
åã®[è¡çªå·,ãã®è¡ã®æåå]ã§ããã
sub lineOutFnc ( $ ) ; # lineOutã®ãããªé¢æ°åããã¤ãã®ãã¡ãã©ããã使ãããããã³ãã¬ã¼ã..ã¨è¨ãã°è¯ãããª.
sub lineOutColon ( $ ) { $oc++ ; $_[0][0], ":\t", $_[0][1] } # è¡çªå·ã«ã³ãã³(:) ãä»å ãã¦åºå
sub lineOutCouleur ( $ ) { $oc++ ; color($o{':'}) . "$_[0][0]:" . color('reset') . "\t", $_[0][1] } # G
sub lineOutBlunt ( $ ) { $oc++ ; $_[0][1] }
sub lineOutTime ( $ ) { sprintf("%02d:%02d:%02d\t", @{[localtime]}[2,1,0]), lineOutFnc $_[0] } # <- ããã ãlineOutFncãä¸ã«å«ãã
* lineOutFnc = ($o{':'}//'')eq"0"? * lineOutBlunt : (exists$o{':'}) ? * lineOutCouleur : * lineOutColon ;
* lineOut = $o{t}? *lineOutTime : * lineOutFnc ;
@nums0 = do {
my %t =(0=>['Inf'],1=>[1],2=>[1,2,4,8],5=>[1,2,5],7=>[1,1.5,2,3,5,7],8=>[1,1.5,2,3,5,8],9=>[1..$o{b}-1]) ;
do { say STDERR BOLD RED "-p should has a parameter in {0,1,2,5,7,8,9}" ; exit } if exists $o{p} && !defined $t{$o{p}} ;
my @t = @{$t{$o{p}//1}} ;
grep {$_ < $o{b} || $_ == 'Inf'} @t
} ;
& procFiles ; # è¤æ°ã®ãã¡ã¤ã«ãæ³å®ãã¦å¦çã
exit 0 ;
sub procFiles ( ) { # è¤æ°ã®ãã¡ã¤ã«ãæ³å®ãã¦å¦ç
my $files = @ARGV ;
do {
my $fileName = shift @ARGV ; # ãã¡ã¤ã«å
my $FH ; # ãã¡ã¤ã«ãã³ãã«
open $FH , "<" , $fileName or warn "File `$fileName' does not open." and next if defined $fileName ;
$FH = *STDIN if ! defined $fileName ;
binmode $FH , ":gzip(autopop)" if $o{z} ; # <-- autopop ã¨ã¯?
say UNDERLINE $fileName if $files >= 2 ; # ãã¡ã¤ã«ã2å以䏿å®ããã¦ãããããã¡ã¤ã«åã表示ããã
& eachFile ( $FH ) ;
} while @ARGV
}
sub eachFile ( $ ) { # 1åã®ãã¡ã¤ã«ã«å¯¾ããå¦ç(弿°ã¯ãã¡ã¤ã«ãã³ãã«ãSTDINã®å ´åãããã)
@nums = @nums0 ;
my $FH = $_[0] ;
my $ready ; # ãã®æ°ãæ£ãªãåºåãããâ ä»çµã¿ã¯ãããªã®è¯ãæ°ãªã©ã®ããªã¬ã¼ã«ãããä¸å®å¤ãæ ¼ç´ããã¦ã1ãã¤æ¸ãã
my @stockLines = () ; # ããã¤ãã®è¡ã®ãæååãæ ¼ç´ããã
$oc = 0 ; # åºåããè¡ã®æ°ãæ°ããã®ã§ããã§ãªã»ããã
do { while ( <$FH> ) { push @stockLines , [ $. , $_ ] ; last if @stockLines >= $o{e} } } if $o{e} > 0 ;
print lineOut $_ for @stockLines ;
my $firstEnd = @stockLines ? $stockLines[-1][0] : 0 ; # æåã®æ®µéã§èªãã æå¾ã®è¡ã®è¡çªå·
# 2. æ¡ä»¶ã«ä¸è´ãããã®ã®ã¿åºåããã
while ( <$FH> ) {
push @stockLines , [ $. , $_ ] ; # pushã¨shiftã対ã«ãã¦FIFOã®ãããªä»çµã¿
my $theNext = shift @stockLines ;
$ready = fitCheck $$theNext[0] ? $o{A} + 1 : 0 ;
print lineOut $theNext if $ready -- > 0 && $$theNext[0] > $firstEnd ;
}
# 3. æ®ã£ã¦ãããã®ãæ¸ãåºã
print lineOut $_ for grep { $$_[0] > $o{e} } @stockLines ;
# 4. æ¨æºã¨ã©ã¼åºåã«äºæ¬¡æ
å ±ãæ¸ãåºãã
my $n = $. =~ s/(?<=\d)(?=(\d\d\d)+($|\D))/,/gr ;
my $msg = "-- $n lines are read and $oc lines are output. To suppress this message on STDERR, run as `$Script -20'. " ;
say STDERR FAINT BOLD $msg if 0 ne ($o{2}//'') ;
}
sub fitCheck ( $ ) { # è¡çªå·ã弿°ã«ä¸ãããããã¨ããããåºåå¯è½ã«è©²å½ããããå¤å®ããã@numsãæé»ã«æä½ãã¦ããã
my $head = shift @nums ; # æ°ç ãåããããªã¤ã¡ã¼ã¸ã§å¦çããã
while ( $head < $_[0] ) { push @nums , $head * $o{b} ; $head = shift @nums }
if ( $head < $_[0] + 1 ) { push @nums , $head * $o{b} ; return $_[0] >= $o{f} } #
unshift @nums, $head ;
return $_[0] >= $head - $o{B} && $_[0] <= $head + $o{A} && $_[0] >= $o{f} ;
}
## ãã«ãã®æ±ã
sub VERSION_MESSAGE {}
sub HELP_MESSAGE {
use FindBin qw[ $Script ] ;
$ARGV[1] //= '' ;
open my $FH , '<' , $0 ;
while(<$FH>){
s/\$0/$Script/g ;
print $_ if s/^=head1// .. s/^=cut// and $ARGV[1] =~ /^o(p(t(i(o(ns?)?)?)?)?)?$/i ? m/^\s+\-/ : 1;
}
close $FH ;
exit 0 ;
}
=encoding utf8
=head1
$0 [-z] [-B 0] [-A 0] [-p 1] [-f 1] [-e 2]
大ããªããã¹ããã¡ã¤ã«ã®å
¨ä½ãææ¡ããããããããã
æåã¨æå¾ã®æ°è¡ã¨éä¸ã® 1, 10, 100, 1000 .. è¡ç®ãªã©ãåºåãããã©ãåºåãããã¯ããªãã·ã§ã³ã§æå®å¯è½ã
ãªãã·ã§ã³:
-b N :次ã®èª¬æã® 10,100,1000,.. ã®æ°ã®é¨åã夿´ã㦠N ã®ã¹ãä¹ã«ãªããããã«ããå
ã®æ°ã®å
N以ä¸ã®ã¿ã«éå®ã(base)
-p 1 : ããªã®è¯ãæ°ã 1, 10, 100, 1000 .. è¡ã«éå®ããã(-bã®è¨å®ã§å¥ã®æ°ã®ã¹ãä¹ã«å¤æ´å¯è½ã)
-p 2 : ããªã®è¯ãæ°ã 1, 2, 4, 8, 10, 20, 40, 80 .. ã¨ãã1,2,4,8ãã®1å,10å,100å.. ã¨ããã
-p 7 : ããªã®è¯ãæ°ã ã1, 1.5, 2, 3, 5, 7 ãã®1å,10å,100å.. ã¨ããã
-p 8 : ããªã®è¯ãæ°ã ã1, 1.5, 2, 3, 5, 8 ãã®1å,10å,100å.. ã¨ããã
-p 9 : ããªã®è¯ãæ°ã ä¸1æ¡ä»¥å¤ãå
¨ã¦0ã®æ°ã¨è¦ãªã(ã1,2,3,4,5,6,7,8,9ãã®1å,10å,100å..)ã
-p 0 : ä¸è¨ã®åä½ãããªããæ¬¡ã® -e ã§æå®ãããé¨åã®ã¿ãåºåã
-e num : å
¥åå
¨ä½ã®æåã¨æå¾ã num è¡åºåããããã«ãããæªæå®ã§ããã° num = 2ã0ãæå®å¯è½ã (edge)
-f num : éå§è¡ã®æå® (from)
-: [0|colorname] : è¡çªå·ã®ã¿è²ãçè²ããæå®ãblue ã "bold yellow"ãªã©ãæå®å¯è½ã0 ãæå®ããã¨è¡çªå·ãåºåããªãã
-B num ããã®è¯ãæ°ã®ä½è¡åãã表示ããããé£ç¶è¡¨ç¤ºã«ç¨ããã(grepã³ãã³ãã® -B ã¨åæ§ãªåä½)
-A num ããã®è¯ãæ°ã®ä½è¡å¾ã¾ã§è¡¨ç¤ºããããé£ç¶è¡¨ç¤ºã«ç¨ããã(grepã³ãã³ãã® -A ã¨åæ§ãªåä½)
-2 0 : ãä½è¡èªãã§ä½è¡åºåãããããæ¨æºã¨ã©ã¼åºåã«åºãã¦ããã®ãæå¶ããã
-t : åºåæã®æå»æ
å ±ã è¡é ã«ä»å ã
-z : å
¥åããgzipã®å½¢å¼ã§å§ç¸®ããã¦ãããã¨ãæå®ããã
--help : ãã® $0 ã®ãã«ãã¡ãã»ã¼ã¸ãåºãã perldoc -t $0 | cat ã§ãã»ã¼åãã
--help opt : ãªãã·ã§ã³ã®ã¿ã®ãã«ããåºããopt以å¤ã§ã options ã¨å
é ã1æå以ä¸ä¸è´ããã°è¯ãã
éçºä¸ã®ã¡ã¢:
* ãã¼ãã¼ãããã®å
¥åå¾
ã¡ã®å ´åã¯ã-tã§æ¤åºãã¦ãALRMã·ã°ãã«ã§åä»ãä¿ãããã«ãããã
* -: [colorName|0] ã§è¡çªå·ã®åºåãå¶å¾¡ãããã-g 㨠-q ã®æ©è½ãä¸ã¤ã«ãããã¨ã«ãªãã
* -bã¨-pãçµ±åãããã-b n1,n2 ã¨ã㦠n1ãåºæ°ãn2ãæ¢ã«-pã§æå®ãããéãã«ãããã
* è¤æ°ã®ãã¡ã¤ã«ã®ä¸èº«ãåºãã¨ãã«ããã¡ã¤ã«åã«ä¸ç·ãå¼ãããã«ããããä¸ç·ã«ããã«ããªãã·ã§ã³ã§å¤æ´å¯è½ã«ãããããã
* æå»æ
å ±ããã£ã¨æ£ç¢ºã«åºããããããTime::HiResã使ã£ã¦ãã³ã¢ã¢ã¸ã¥ã¼ã«ãã©ããã確ãããä¸ã§å®è£
ãããã
=cut
( run in 0.621 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )