Skip to content

Instantly share code, notes, and snippets.

@seaneshbaugh
Created April 17, 2012 06:08
Show Gist options
  • Save seaneshbaugh/2403827 to your computer and use it in GitHub Desktop.
Save seaneshbaugh/2403827 to your computer and use it in GitHub Desktop.
File Chopper
require "optparse"
require "ostruct"
class FileChopper
Usage = "Usage:
chop.rb FILE[S] [options]
Options:
-f, [--front] # Chop from the front
-l, [--lines] # Chop from each line
-N, [--no-newlines] # Do not preserve newlines in line mode
-n, [--number=NUMBER] # Number of characters to chop
-p, [--pretend] # Run but do not make any changes
-q, [--quiet] # Supress status output
-V, [--verbose] # Show extra output
-h, [--help] # Show this help message and quit
-v, [--version] # Show chop.rb version number and quit
Description:
This script will chop bytes from a file. The default is to chop one
character from the end of the file. Any number of characters can be
chopped from the beginning or end of the file or from each line.
Example:
chop.rb myfile.txt -f -n 4
This chops the first four characters from the file."
Version = "0.1.0"
def initialize
@options = OpenStruct.new
@file_list = []
@options.files = []
@options.back = true
@options.front = false
@options.lines = false
@options.no_newlines = false
@options.number = 1
@options.pretend = false
@options.quiet = false
@options.verbose = false
@options.help = false
@options.version = false
end
def parse_options(argv)
if argv.length == 0
puts "Error: no file(s) given\n\n"
puts Usage
exit
end
begin
op = OptionParser.new do |ops|
ops.banner = Usage
ops.separator ""
ops.on("-f", "--front", "Chop from the front") do |front|
@options.front = front
end
ops.on("-l", "--lines", "Chop from each line") do |lines|
@options.lines = lines
end
ops.on("-N", "--no-newlines", "Do not preserve newlines in line mode") do |no_newlines|
#no idea why this was coming through as false
@options.no_newlines = true
end
ops.on("-n", "--number NUMBER", "Number of characters to chop") do |number|
@options.number = number.to_i
if @options.number < 1
raise OptionParser::InvalidArgument, "number of characters must be >= 1"
end
end
ops.on("-p", "--pretend", "Run but do not make any changes") do |pretend|
@options.pretend = pretend
end
ops.on("-q", "--quiet", "Supress status output") do |quiet|
@options.quiet = quiet
end
ops.on("-V", "--verbose", "Show extra output") do |verbose|
@options.verbose = verbose
end
ops.on("-h", "--help", "Show this help message and quit") do |help|
puts Usage
exit
end
ops.on("-v", "--version", "Show chop.rb version number and quit") do |version|
puts "chop.rb #{Version}"
exit
end
end
op.parse!(argv)
rescue Exception => exception
puts "Error: #{exception.message}\n\n"
puts Usage
exit
end
if argv.length == 0
puts "Error: no file name given.\n\n"
puts Usage
exit
end
@file_list = argv
end
def chop
@file_list.each do |file_name|
print_message "Debug", file_name
if @options.lines
content = IO.readlines(file_name)
else
content = [open(file_name, "rb") { |file| file.read }]
end
result = ""
content.each do |line|
newline = false
if @options.lines
if line[-1, 1] == "\n"
newline = true
line = line[0, line.length - 1]
end
end
print_message "Debug", line.gsub(/[[:cntrl:]]|[[:space:]]/, "_")
if @options.number > line.length
@options.number = line.length
end
if @options.front
result << line[@options.number, line.length]
else
result << line[0, line.length - @options.number]
end
if @options.lines && !@options.no_newlines
if result.length == 0
result = "\n"
else
#if line[-1, 1] == "\n" && result[-1, 1] != "\n"
if newline && result[-1, 1] != "\n"
result << "\n"
end
end
end
end
print_message "Debug", result.gsub(/[[:cntrl:]]|[[:space:]]/, "_")
unless @options.pretend
File.open(file_name, "wb") do |output|
result.each_byte do |byte|
output.print byte.chr
end
end
end
end
end
def print_message(type, message)
if (type == "Error" || @options.verbose) && !@options.quiet
puts "#{type}: #{message}"
end
end
end
file_chopper = FileChopper.new
file_chopper.parse_options(ARGV)
file_chopper.chop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment