App-Bin4TSV
view release on metacpan or search on metacpan
scripts/headomit view on Meta::CPAN
#!/usr/bin/perl
use 5.014 ; use strict ; use warnings ;
use Getopt::Std ; getopts ':!_12cfh:t:v' , \my%o ;
use Term::ANSIColor qw[ :constants ] ; $Term::ANSIColor::AUTORESET = 1 ;
use FindBin qw[ $Script ] ;
use feature qw[ say ] ;
sub subinfo ( ) ;
sub show ;
$o{h} //= 1 ;
my $fc = 0 ; # ããã¾ã§ã«èªãã ãã¡ã¤ã«ã®æ° (file count)
my $pc = 0 ; # æ¨æºåºåã«åºåããè¡æ° (print count)
my $ic = 0 ; # å
¥åããè¡æ° (input count)
$| = 1 if $o{'!'} ;
& main ;
#subinfo ;
END { subinfo } ;
exit ;
sub main ( ) {
my $head = '' ; # æåã®è¡
* show0 = sub { print "$_" ; $pc ++ } ;#$o{f} ?
* flag_n = $o{':'} ? sub { $_ = "$.:\t$_" } : sub {} ;
* flag_f = $o{f} ? sub { $_ = "$ARGV\t$_" } :sub {} ;
* show = $o{2} ? sub { & show0 if $_ ne $head } : * show0 ;
my $slen = $o{t} // undef ;
my @s = () ; #undef) x $slen if defined $slen ; # èªãã è¡ãä¸å®ã®é·ãèããã
#* stock = defined $o{t} ? sub { push @s , $_ ; $_ = @s > $slen ? shift @s : undef ; goto LOOP_END unless defined $_ } : sub {} ;
* stock = defined $o{t} ? sub { push @s , $_ ; $_ = @s > $slen ? shift @s : goto LOOP_END } : sub {} ; # goto ãããªããã¼ã ãæå³éãåä½ã
* flag_c = $o{c} ? sub { } : sub { $ic += $. ; $. = 0 ; 1 } ;
START1 :
#exit eof () ;
do { $head .= <> ; if ( eof ) { ++$fc ; &flag_c ; exit if eof () ; goto START1} } for 1 .. $o{h} ;# ; $lc ++ } ;
do { $_ = $head ; & show0 } if $o{1} ; # æåã®ãã¡ã¤ã«ã ãå
é è¡ã表示ããã
EACH_INPUT_LINE :
while ( <> ) {
& flag_n ;
& flag_f ;
& stock ;
show ;
LOOP_END :
if ( eof ) {
@s = () ;
& flag_c ; #do{ $ic += $. ; $. = 0 } if ! $o{c} ;
$fc ++ ;
unless ( eof () ) { # æå¾ã®ãã©ã«ãã§ç¡ãéã
#do { my $t = <> for 1 .. $o{h} } if ! $o{2} ;
START2 :
exit if eof () ;
do { do { my $t = <> ; if (eof) { ++$fc ; &flag_c ; goto START2 } } for 1.. $o{h} } if ! $o{2} ;
say '' if $o{'_'} ;
}
}
}
}
sub d3 { $_[0] =~ s/(?<=\d)(?=(\d\d\d)+($|\D))/,/gr }
sub subinfo ( ) {
$ic += $. ;
$_ = d3 $_ for $ic, $pc, $fc ;
print STDERR DARK ITALIC BOLD CYAN "# of files: $fc, lines output/input: $pc/$ic. ($Script)\n" if $o{v} ;
}
# ãã«ãã®æ±ã
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 ãã¡ã¤ã«1 ãã¡ã¤ã«2 ãã¡ã¤ã«3 ..
cat file | $0
æ©è½:
åãã¡ã¤ã«ã®å
é è¡ã¯åºåããªãã§ãé£çµããã
é£çµã®ç¶ãç®ã¯ãç´åã®ãã¡ã¤ã«ã«æ¹è¡æåãç¡ããã°ãèªåçã«è¿½å ãããã
ãªãã·ã§ã³:
-h N : æåã®Nè¡ã表示ããªããæªæå®ãªã1ã
-t N : åãã¡ã¤ã«ã®æ«å°¾Nè¡ãåºåããªãã
-1 ; æåã®ãã¡ã¤ã«ã«ã¤ãã¦ã¯ãå¿
ãå
é è¡(-hã§æå®ããNè¡)ã表示ããã(!! ãã ãä»ã®æã-:ã¨-fã®ã©ã¡ãã¨ããã¾ããã£ã¦ãªã!! )
-2 ; åºåããªãã®ã¯ãåãã¡ã¤ã«ã®å
é è¡ã¨ããã«ã¼ã«ãããæåã®ãã¡ã¤ã«ã®å
é è¡ã¨æååã¨ãã¦ä¸è´ããè¡ãã«å¤æ´ããã(-h ã§N>1ã§ã¯ãã¾ããããªã)
-_ ; åãã¡ã¤ã«ã®æå¾ã§ 空è¡ãåºåã
( run in 0.582 second using v1.01-cache-2.11-cpan-ceb78f64989 )