File Splitter

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 ,  | Tags  | no comments

String Iterator

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  | Tags ,  | no comments

Getting Ruby include path

Posted by root Sun, 13 Jan 2008 14:25:00 GMT

$LOAD_PATH === $: #true, hence
irb
puts$:.sort.join($/)

__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

Posted in  | Tags  | no comments

File.rename(tedious part of the filename)

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 ,  | Tags  | no comments

Sort By Part Of Filename

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+$/)

# 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

Posted in ,  | Tags  | no comments

Dynamically Define Methods

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 ,  | Tags ,  | no comments

perldoc usage

Posted by root Wed, 09 Jan 2008 10:59:00 GMT

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

Posted in  | Tags  | no comments

Special characters in file name

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  | Tags  | no comments

Just_Another_Perl_Hacker.pm

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  | Tags  | no comments

"Argument list too long"

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  | Tags  | no comments

Older posts: 1 2