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
}

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

Six happy numbers

Jan 07
2008
numbers = Hash.new
loop{numbers[rand(50)]=true && numbers.keys.size==6 && break}
p numbers.keys.sort.join(%q/ /)

#or just

p (1..49).to_a.sort_by{rand}.[](1..6)

bashrc

Dec 29
2007
export PATH=$PATH:$HOME/bin:/usr/sbin:/sbin:$HOME/network
export TERM=linux
export gemdoc=`gem environment gemdir`/doc
 
function prompt_set {
 
 local GRAY="\[\033[1;30m\]"
 local LIGHT_GRAY="\[\033[0;37m\]"
 local CYAN="\[\033[0;36m\]"
 local LIGHT_CYAN="\[\033[1;36m\]"
 local NO_COLOUR="\[\033[0m\]"
 
 case $TERM in
    xterm*|rxvt*)
        local TITLEBAR='\[\033]0;\u@\h:\w\007\]'
        ;;
    *)
        local TITLEBAR=""
        ;;
 esac
 
local temp=$(tty)
local GRAD1=${temp:5}
PS1="$GRAY-$CYAN-$LIGHT_CYAN(\
$CYAN\u$GRAY@$CYAN\h$LIGHT_CYAN)$CYAN-$LIGHT_CYAN(\
$CYAN\$(date +%H:%M)$GRAY:$CYAN\w\
$LIGHT_CYAN)$CYAN-$GRAY-$LIGHT_GRAY "
PS2="$LIGHT_CYAN-$CYAN-$GRAY-$NO_COLOUR "
}
 
prompt_set
 
alias get_visits="ssh l 'tail /home/postgres/stats.txt'"
alias __='history | tail -2 | head -1'
alias r="temproot `id -u`'"
alias sql='mysql --password=pass'
alias ..='cd ..';
alias ...='cd ../..';
alias ,='cd -'
alias e=exit
alias e=exit
alias v=vim
alias l='ls -lc -h --color=yes'
alias c=clear
alias top='top -d1'
alias hc='history -c'
alias gre=grep
alias gr=gre
alias gpre=gr
alias grp=gpre
alias le=less
alias mroe=more
alias mreo=mroe
alias h=history
alias pe='perl -e'
alias pc='perl -c'
alias t=date
alias d=date
 
function FOR {
        local count=$1
        start=0
        shift
        while [ $start -lt $count ]
        do
        $*
        sleep 1
                clear
                let start=$start+1
        done
}

nmap cleaner in Perl

Dec 29
2007
$”=@ARGV;/^\D+/||print for`nmap$”`

Ruby one-liners (file manipulation)

Jul 20
2007
# number each line of a file
ruby -ne 'puts "#{$.}\t#{$_}"' file.txt
 
# print all non-blank lines
ruby -pe '$_.chomp.empty? and next' file.txt
 
# number and print all non-blank lines
ruby -ne '$_.chomp.empty? or print $.,"\t", $_' file.txt
 
# number and print each blank line
ruby -ne 'puts $. if $_.chomp.empty?' file.txt
 
# reverse order of lines (`tac` style)
ruby -e 'puts File.open($< .filename).readlines.reverse' file.txt
 
# print matched string from lines, matching the pattern
ruby -ne 'puts $_.scan(/^\w+/)' /etc/passwd
 
# triple space a file and reverse order of lines
ruby -e '$,="\n\n\n"; puts File.readlines($<.filename).reverse.join' file.txt
 
# print first line of a file (emulate 'head -1')
ruby -ne 'puts $_; break' file.txt
ruby -pe '$. == 1 or break' file.txt
 
# print last line of a file (emulates 'tail -1')
ruby -ne 'END{puts $_}' file.txt
 
# print last line number (emulates 'wc -l')
ruby -e 'loop{gets or break}; puts $.' file.txt
 
# print only lines that match a regular expression (emulates 'grep')
ruby -pe 'next if not /regex/' file.txt
 
# print only lines that do not match a regular expression (emulates 'grep -v')
ruby -pe 'next if /regex/' file.txt
 
# print section of file between two regular expressions, /^root/ and /^nobody/
ruby -ne 'puts $_ if /^root/../^nobody/' file.txt
 
# print file and remove duplicate, consecutive lines from a file (emulates 'uniq')
ruby -ne '$_.eql? $; or puts $_;$; = $_;' file.txt
 
# print file except for blank lines
ruby -pe 'next if $_.chomp.empty?' file.txt
ruby -pe 'next if /^\s*$/' file.txt
ruby -pe 'next if $_.split(/\S+/).size < 2' file.txt
 
# print file except for lines, starting with digit (unclear and inefficient)
ruby -pe 'next if (48..57).to_a.include?($_.split(//)[0][0])' file.txt
 
# delete all leading blank lines at top of file
ruby -pe '$,="$." if not $_.chomp.empty?; $, or next' file.txt
 
# print section of file from regex to end of file
ruby -pe '$,="$." if /regex/; $, or next' file.txt
 
# delete leading and trailing whitespace from each line
ruby -pe '$_.strip!.sub!(/$/, "\n")' file.txt
ruby -ne 'puts $_.strip! + $/' file.txt
 
# delete leading whitespace from the beginning of each line
ruby -ne 'puts $_.lstrip! || $_' file.txt
 
# convert DOS newlines (CR/LF) to Unix format (LF)
ruby -i -pe 'sub(/\r\n/, "\n")' file.txt

compare the ip address of different hostnames

Jul 20
2007
require "socket"
 
unless ARGV.size == 2
 raise ArgumentError,
   "Expected two hostnames, got #{ARGV.size}"
end
 
h1,h2 = ARGV
 
begin
  h1,h2 = Socket::getaddrinfo(h1, 7)[0][3], Socket::getaddrinfo(h2, 7)[0][3]
rescue
  print "#{$!}...\texiting\n"
  exit
end
 
if h1.eql? h2
  puts "hosts have the same ip address: #{h1}"
else
  puts "hosts differ: #{h1} #{h2}"
end

nmap output cleaner

Jul 19
2007
arg = ARGV.join(" ")
arg.empty? and exit
 
puts %x[nmap #{arg} 2>/dev/null].scan(/^\d+.+/)

1..666

Jul 19
2007

irb console

$.=0; (1..666).to_a.inject(1){|res, i| res += i}.to_s.split(//).collect{|i| $. += i.to_i}; STDOUT.puts$

Calendar

February 2012
M T W T F S S
« Sep    
 12345
6789101112
13141516171819
20212223242526
272829  

Tags