Simple file hiding

Posted by root Tue, 08 Jan 2008 07:45:00 GMT

irb console
$/ = "\r\n"

# addition of .mp3 file to .gif file
File.open('test.gif', 'a') do |fh| 
  fh << "\r\n#{File.readlines('test.mp3')}"
end

# extraction of the .mp3 content
File.open('extracted.mp3', 'w') do |fh| 
  fh << File.readlines('test.gif')[1..-1]
end

Posted in  | Tags  | no comments

Six happy numbers

Posted by root Tue, 08 Jan 2008 06:22:00 GMT

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)

Posted in  | Tags  | no comments

List Values and Arrays

Posted by root Fri, 30 Nov 2007 14:28:00 GMT

Perl

($a, $b) = (1, 2);
 @arr    = (1, 2);

Python
 a, b = 1, 2
 arr  = 1, 2;

Ruby
 a, b = 1, 2
 arr  = 1, 2
 arr  = 1,2, *[11,22] # the same as 
 arr  = [1,2, [11,22]].flatten
the ’*’ example was stolen from Ola Bini’s blog

Posted in , ,  | Tags  | no comments

Ruby one-liners (file manipulation)

Posted by root Fri, 20 Jul 2007 17:11:00 GMT


# 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

Posted in ,  | Tags ,  | 4 comments

nmap output cleaner

Posted by root Fri, 20 Jul 2007 03:21:00 GMT

arg = ARGV.join(" ")
arg.empty? and exit

puts %x[nmap #{arg} 2>/dev/null].scan(/^\d+.+/)

Posted in ,  | Tags  | no comments

1..666

Posted by root Fri, 20 Jul 2007 02:59:00 GMT

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

Posted in ,  | Tags  | no comments

Older posts: 1 ... 3 4 5