Skip to content

Commit

Permalink
Improved line for piped input. Added trim, a special case of line.
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephPecoraro committed Apr 4, 2009
1 parent d6ab0ee commit 98c2960
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 12 deletions.
47 changes: 35 additions & 12 deletions line
Original file line number Original file line Diff line number Diff line change
@@ -1,19 +1,29 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# Start Date: Sunday January 4, 2009 # Start Date: Sunday January 4, 2009
# Current Version: 0.99 # Current Version: 1
# Author: Joseph Pecoraro # Author: Joseph Pecoraro
# Contact: joepeck02@gmail.com # Contact: joepeck02@gmail.com
# Decription: Grab certain lines of a file # Decription: Grab certain lines of a file
# with a simple syntax (see usage). Silent # with a simple syntax (see usage). Silent
# usage useful for piping output. # usage useful for piping output and Pipe
# option for piping input into it


# Wrapper around functionality # Wrapper around functionality
class FileLines class FileLines


attr_accessor :lines attr_accessor :lines, :stdin


def initialize(filename, options) def initialize(options, filename=nil)
@lines = File.readlines(filename) if filename.nil?
@stdin = true
@lines = []
while line = STDIN.gets
@lines << line
end
else
@stdin = false
@lines = File.readlines(filename)
end
@silent = false @silent = false
parse_options(options) parse_options(options)
end end
Expand All @@ -27,11 +37,14 @@ class FileLines
end end


def parse(args) def parse(args)
puts unless @silent
args.each do |arg| args.each do |arg|


# String: all
if m = arg.match( /^all$/ )
print_lines(0, @lines.size)

# Ranges: 3-7 # Ranges: 3-7
if m = arg.match( /^(\d+)-(\d+)$/ ) elsif m = arg.match( /^(\d+)-(\d+)$/ )
lower = m[1].to_i lower = m[1].to_i
upper = m[2].to_i upper = m[2].to_i
lower = 1 if lower.zero? # 0 => 1 lower = 1 if lower.zero? # 0 => 1
Expand Down Expand Up @@ -73,11 +86,11 @@ class FileLines
print_line( lineno ) print_line( lineno )
end end


puts unless @silent
end end
end end


def print_lines(lower, upper) def print_lines(lower, upper)
return if lower > @lines.size
i = lower i = lower
@lines[lower, upper].each do |line| @lines[lower, upper].each do |line|
if @silent if @silent
Expand Down Expand Up @@ -141,9 +154,12 @@ if $0 == __FILE__
# Pull out the Options # Pull out the Options
options = ARGV.select { |arg| arg.match(/-[a-z]/i) } options = ARGV.select { |arg| arg.match(/-[a-z]/i) }
ARGV.delete_if { |arg| arg.match(/-[a-z]/i) } ARGV.delete_if { |arg| arg.match(/-[a-z]/i) }

# Special Handling for Pipes (don't require a filename)
is_pipe = !STDIN.tty?


# Check Cmd Line Args and Print Usage if needed # Check Cmd Line Args and Print Usage if needed
if ARGV.size <= 1 if ARGV.size <= 1 && !is_pipe
puts "usage: line [options] filename numbers" puts "usage: line [options] filename numbers"
puts puts
puts " options:" puts " options:"
Expand All @@ -155,21 +171,28 @@ if $0 == __FILE__
puts " -1 Prints the last line, (negative)" puts " -1 Prints the last line, (negative)"
puts puts
puts " extra formats:" puts " extra formats:"
puts " all Prints out all lines"
puts " ~5 Prints 2 (default) lines before and after 5" puts " ~5 Prints 2 (default) lines before and after 5"
puts " 4~10 Prints 4 lines before and after 10" puts " 4~10 Prints 4 lines before and after 10"
puts " *7 or 8* Prints all lines before or after the number" puts " *7 or 8* Prints all lines before or after the number"
puts " 5/1 Prints 5 lines, then skips 1 line..." puts " 5/1 Prints 5 lines, then skips 1 line..."
puts " 2:5/1 Starts at line 2, prints and skips..." puts " 2:5/1 Starts at line 2, prints and skips..."
puts puts
exit 1 exit 1
end


# Pipe Usage
if is_pipe
FileLines.new(options).parse(ARGV)

# Unreadable File # Unreadable File
elsif !File.readable?(ARGV[0]) elsif !File.readable?(ARGV[0])
puts "line: file '#{ARGV[0]}' did not exist or was unreadable." puts "line: file '#{ARGV[0]}' did not exist or was unreadable."
exit 1 exit 1
end


# Run the Program # Run the Program
FileLines.new(ARGV[0], options).parse(ARGV[1, ARGV.size]) else
FileLines.new(options, ARGV[0]).parse(ARGV[1, ARGV.size])
end


end end
44 changes: 44 additions & 0 deletions trim
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env ruby
# Author: Joseph Pecoraro
# Date: Saturday April 4, 2009
# Description: Opposite of Head.
# Removes the first -n lines.

# Line to trim up to
limit = 0

# Get a Command line Limit
ARGV.each do |arg|
if matches = arg.match( /^-(\d+)$/ )
limit = matches[1].to_i
end
end


# Remove Command Line Args
ARGV.delete_if { |arg| arg.match(/^-\d+$/) }

# Print Lines from STDIN
if ARGV.size == 0

# Skip Lines
while limit > 0
limit -= 1
gets
end

# Print Loop
while ( line = gets )
puts line
end

# Print Lines from a File
else

ARGV.each do |filename|
lines = File.readlines(filename)
lines.slice!(0, limit)
lines.each { |l| puts l }
end

end

0 comments on commit 98c2960

Please sign in to comment.