Posted by root
Sat, 19 Jan 2008 05:28:00 GMT
use strict;
use Getopt::Std;
use File::Basename;
getopts('scf:p:');
our($opt_s, $opt_c, $opt_f, $opt_p);
my $file = $opt_f;
my $size = $opt_p || 1;
my $len = 1024;
my $c = 1;
my ($buf, $counter);
-f $file or usage();
if($opt_s){
$size = 1024 * $size;
open IN, "< $file" or die "cannot open_r $file $!";
open OUT, "> $file.$c" or die "cannot open_w $file $!";
binmode IN;
binmode OUT;
while(read(IN, $buf, $len)){
$counter++;
if($counter > $size){
$counter = 0;
$c++;
close OUT;
open OUT, "> $file.$c" or die "cannot open_w $file $!";
binmode OUT;
}
print OUT $buf
}
} elsif($opt_c) {
my @files = grep { -f and /^$file\.\d+$/ } glob '*';
my $newfile = "splitter_$file";
open OUT, ">> $newfile" or die "cannot open_w $file $!";
binmode OUT;
map { $_ =~ s/^$file\.// } @files;
for(sort {$a<=>$b} @files ){
open IN, "$file.$_" or die "cannot open_r $file $!";
binmode IN;
print OUT $_ while <IN>;
close IN;
}
} else {
usage()
}
sub usage{
my $pro = basename($0);
print <<SQ;
$pro (-s|-c) -p piece size -f filename
-s split a file into pieces
-c collect a file from pieces
-p chunk size (defaults to 1MB)
-f file to be processed
SQ
exit 1
}
Posted in Perl, scripts | Tags splitter | no comments
Posted by root
Mon, 14 Jan 2008 22:08:00 GMT
class String
class Iterator < String
def initialize(str='', next_char=0)
@next_char = next_char
super(str)
end
def next
return nil if @next_char > self.size
@next_char += 1
get_char(-1)
end
def prev
return nil if @next_char <= 0
@next_char -= 1 if @next_char > 0
get_char()
end
private
def get_char(pos=0)
if block_given?
yield split(//)[@next_char+(pos)]
else
split(//)[@next_char+(pos)]
end
end
end
end
s = String::Iterator.new "abcdef"
while char = s.next
puts char
end
while char = s.prev
puts char
end
Posted in Ruby | Tags patterns, senseless | no comments
Posted by root
Sun, 13 Jan 2008 14:25:00 GMT
irb
Posted in Ruby | Tags tools | no comments
Posted by root
Sun, 13 Jan 2008 07:58:00 GMT
Rename each file in each sub-directory, substituting ‘%20’ with ’ ‘(space).
class File
def self.rename_basename(abs_name, old_str, new_str)
f_name = File.basename(abs_name).gsub Regexp.quote(old_str), new_str
d_name = File.dirname(abs_name)
File.rename(abs_name, d_name + '/' + f_name)
rescue
raise ArgumentError, "No such file #{filename}", caller
end
end
Dir["*/**"].each do |f|
if File.basename(f).match('%20')
File.rename_basename(f, '%20', ' ')
end
end
Posted in Ruby, scripts | Tags tools | no comments
Posted by root
Sat, 12 Jan 2008 08:57:00 GMT
Assume that we have directory, containing files like:
part_1.txt
part_2.txt
part_100.txt
part_200.txt
In case that we are interested in /(\d+)\.\w+$/ as part of the sorting criteria:
Perl
# short but unefficient
@a = sort {($a =~ /(\d+)\.\w+$/)[0] <=> ($b =~ /(\d+)\.\w+$/)[0]} <*>;
Ruby
regex = Regexp.new(/(\d+)\.\w+$/)
Dir["*"].grep(regex).sort do |a, b|
a.match(regex)[0].to_i <=> b.match(regex)[0].to_i
end
Dir["*"].grep(regex).sort_by do |name|
name.match(regex)[1].to_i
end
Posted in Ruby, Perl | Tags sorting | no comments
Posted by root
Fri, 11 Jan 2008 07:21:00 GMT
Perl
package MyClass;
package main;
*MyClass::from_my_class = sub {
print "defined in ", __PACKAGE__, "\n"
};
MyClass::from_my_class();
Ruby
class String
def self.from_string
print "defined in " + self.name + $/
end
end
String::from_string
String. from_string
Posted in Ruby, Perl | Tags methods, OOP | no comments
Posted by root
Wed, 09 Jan 2008 10:59:00 GMT
function manual:
search the text in perlfaq:
module content:
module absolute filename:
and of course:
Posted in Perl | Tags shortcuts | no comments
Posted by root
Wed, 09 Jan 2008 07:43:00 GMT
Safely manipulate file with special characters in the name:
ruby -rfileutils -e 'FileUtils.rm_r "~"'
Posted in Ruby | Tags administration | no comments
Posted by root
Wed, 09 Jan 2008 05:03:00 GMT
package Just_Another_Perl_Hacker; $$
perl -MJust_Another_Perl_Hacker -e 's|(?<=\w)_| |g && tr/:://d && print for keys %main::'
Posted in Perl | Tags japh | no comments
Posted by root
Wed, 09 Jan 2008 03:08:00 GMT
Given that linuxjournal has some good points, there is no need for another solution, except in the cases when you operate on different machine. Here is oneliner solution:
irb console #remove Opera cached files
too_many_files = "~/.opera/cache4/"
Dir.new(too_many_files_dir).each do |f|
File.file?(f) && File.unlink(f)
end
Posted in Ruby | Tags administration | no comments