File Splitter

Jan 18
2008
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
}

String Iterator

Jan 14
2008
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

Getting Ruby Include Path

Jan 13
2008
$LOAD_PATH === $: #true, hence

irb

puts$:.sort
 
__END__
.
/usr/lib/ruby/1.8
/usr/lib/ruby/1.8/i386-linux
/usr/lib/ruby/1.8/i486-linux
/usr/local/lib/site_ruby
/usr/local/lib/site_ruby/1.8
/usr/local/lib/site_ruby/1.8/i386-linux
/usr/local/lib/site_ruby/1.8/i486-linux

File.rename(tedious part of the filename)

Jan 13
2008

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

Sort By Part Of Filename

Jan 12
2008

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+$/)
 
# enum.sort {| a, b | block } => array
Dir["*"].grep(regex).sort do |a, b|
  a.match(regex)[0].to_i < => b.match(regex)[0].to_i
end
 
# enum.sort_by {| obj | block } => array
Dir["*"].grep(regex).sort_by do |name|
  name.match(regex)[1].to_i
end

Dynamically Define Methods

Jan 11
2008

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

perldoc usage

Jan 09
2008

function manual:

perldoc -f grep

search the text in perlfaq:

perldoc -q grep

module content:

perldoc -m IO::Socket

module absolute filename:

perldoc -l IO::Socket

and of course:

perldoc perl

Special characters in file name

Jan 09
2008

Safely manipulate file with special characters in the name:

ruby -rfileutils -e 'FileUtils.rm_r "~"'

Just_Another_Perl_Hacker.pm

Jan 08
2008
package Just_Another_Perl_Hacker; $$
perl -MJust_Another_Perl_Hacker -e 's|(?< =\w)_| |g && tr/:://d && print for keys %main::'

“Argument list too long”

Jan 08
2008

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

Calendar

January 2008
M T W T F S S
« Dec   Feb »
 123456
78910111213
14151617181920
21222324252627
28293031  

Tags