CLI-TextLines-Utils
view release on metacpan or search on metacpan
CLI-TextLines-Hack
The following CLI commands each written as a stand-alone Perl program are provided.
alluniq -- Is every line different from others? If not, how many are $n$-plet occur for each $n$?
idmaker -- Identical numbers are attached to each line. Same ID for the same line contents.
shuffler -- shuffles the lines randomly. With options of taking $n$ lines from the top.
hashtotal -- Calculates the total sum of the first 4 bytes of SHA1 of each line.
ngram -- character-wise $n$-gram ranking (experimental; not fully implemented as planed).
utf8bom -- you can check the existence and add/delete UTF-8 BOM on files.
digitdist -- Enables to see the concrete values based on lengths and character positions.
INSTALLATION
To install this module, run the following commands:
perl Build.PL
./Build
./Build test
./Build install
scripts/utf8bom view on Meta::CPAN
use Getopt::Std ; getopts '!bi:qABDF1' , \my%o ;
use Term::ANSIColor qw[ :constants ] ; $Term::ANSIColor::AUTORESET = 1 ;
use File::Temp qw/tempfile tempdir/ ;
use FindBin qw[ $Script ] ;
do {select STDERR ; print STDERR BRIGHT_RED "Please give argument(s) to $Script.\n" ; & HELP_MESSAGE } if ! @ARGV ;
& eachFile ($_) for @ARGV ;
exit 0 ;
sub eachFile ( $ ) {
my $bom = "\xef\xbb\xbf" ; # <- UTF8 ã® BOM
my $ifn = $_[0] ; # å¦ç対象ã®1åã®ãã¡ã¤ã«ã®åå
my $bak = $o{'!'} ? undef : $o{i} // '.bak' ; # ããã¯ã¢ããã®æ¡å¼µåã
# .bakãå
ã«æ»ãå¦ç
if ( $o{B} ) {
do { print "Specification to $ifn is nullfied for safety.\n"; return } if $ifn =~ m/$bak$/ && ! $o{F} ;
do{ print "$ifn$bak does not exist.\n" ; return } if ! -e $ifn.$bak ;
my $ofn = tempfile ;
rename $ifn, $ofn ;
if ( rename $ifn.$bak, $ifn ) {
scripts/utf8bom view on Meta::CPAN
$ifn =~ s/\/?$/\// if -d $ifn ; # å¾ã§è¦åãåºãæã«ããã£ã¬ã¯ããªã§ãããã¨ã示åããããã
do { print CYAN qq[Not a plain file : `$ifn'.\n] ; return } if ! -f $ifn ; # æ®éã®ãã¡ã¤ã«ã§ã¯ãªãå ´å
open my $ifh , '<', $ifn or do { print STDERR BRIGHT_RED qq[File "$_[0]" does not open.\n] ; return } ;
$_ = <$ifh> ; # ã¾ã1è¡ç®ãèªã
do { print CYAN qq[Cannot be read anything : `$ifn'.\n] ; return } if ! defined $ifn ; # ä½ãèªããªãå ´å
if (! $o{A} && ! $o{D}){
my $p = index ( $_, $bom ) ;
print "BOM (utf-8) " ;
print $p == 0 ? YELLOW "exists " : $p == -1 ? "non-existent " : "irregularly laid" ;
do { my $B = -s $ifn ; $ifn = "$ifn\t(${B} bytes)" } if $o{b} ;
print ": `$ifn'\n" ;
return ;
} ;
do { print CYAN "Skipped the process on : $ifn ($bak in the file name)\n" ; return } if ! $o{F} && $ifn =~ m/$bak$/ ; # ããã¯ã¢ããç¨ã«è¦ãããã¡ã¤ã«
do { print CYAN qq[A symbolic link file : `$ifn'.\n] ; return } if -l $ifn ; # ãªã³ã¯ãã¡ã¤ã«ã®å ´å
do { print CYAN qq[Already has].($o{D}?' omitted BOM':' BOM ').qq[ : File ``$ifn''.\n] ; return }
if index ($_ , $bom) != ($o{D} ? 0 : -1 ) ; # æ¢ã«å¦çæ¸ã¿ã§ä½ãããå¿
è¦ãç¡ãã£ãå ´å
my ($ofh, $ofn) = tempfile ; binmode $ofh ; # æ¸ãè¾¼ããã¡ã¤ã«ã®æºå
$_ = $o{D} ? substr ( $_ , 3 ) : $bom . $_ ; # BOMãä»ãããå¤ãããããå¦ç
do { print {$ofh} $_ } while <$ifh> ; # do {..} while æ§æãå¿
ã1åã¯å®è¡ããã
close $ofh ;
rename $ifn, $ifn . $bak if defined $bak ;
rename $ofn, $ifn ;
print "Processed : `$ifn'. " . (defined $bak ? "(`$ifn$bak' is left as original.)" : '') if ! $o{q} ;
print "\n" if ! $o{q} ;
}
## ãã«ãã¨ãã¼ã¸ã§ã³æ
å ±
scripts/utf8bom view on Meta::CPAN
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
UTF-8 ã® BOM (Byte Order Mark) ããããã©ãããå¤å®ããããªãã·ã§ã³ã«ããæ¸ãããæ¶ãããããã
$0 ãã¡ã¤ã«å [ãã¡ã¤ã«å] [ãã¡ã¤ã«å] ..
夿°ã®ãã¡ã¤ã«ãå¦çããã«ã¯ãfind . | xargs -n 100 $0 ã®ããã«æå®ããã¨è¯ãã ããã
ãªãã·ã§ã³ :
-A : æå®ããåãã¡ã¤ã«ã« UTF-8ã®BOM ã追å ããã
-D : æå®ããåãã¡ã¤ã«ã« UTF-8ã®BOM ãé¤å»ããã
-B : æå®ãããã¡ã¤ã«åBã«å¯¾ãB.bakãBã¸ãBãããã°B.bakã¸ã-i ã§.bakã®é¨åã¯å¤æ´å¯è½ã
-F : -i ã§ããã¯ã¢ãããããã¨æãããæ§ãªãç¹å®ã®æ¡å¼µåãæã£ã¦ãã¦ãããã®ãã¡ã¤ã«ã«å¦çãããã(forced)
-! : å
ã®ãã¡ã¤ã«ãæ®ããªãã
-b : ãã¤ããµã¤ãºãåºåããã(ç¾ç¶ -Aã¾ãã¯-Dãæå®ãããå ´åã¯ç¡å¹ã)
-i str : å
ã®ãã¡ã¤ã«ã«å¯¾ãã¦ï¼æå®æååã®æ¡å¼µåãä»å±ãã¦æ®ããæªæå®ãªãã".bak"ã
-q : ã©ã®ãã¡ã¤ã«ãå¦çããï¼ã¨è¨ããããªæ
å ±ã®åºåãæå¶ããã
éçºä¸ã®ã¡ã¢ :
* ï¼ãã¤ãã®ãã¡ã¤ã«ã«å¯¾ããå¦çãï¼é©åã«è¡ãããã«ãããã
( run in 0.287 second using v1.01-cache-2.11-cpan-e9daa2b36ef )