Skip to content
Newer
Older
100755 105 lines (90 sloc) 2.13 KB
022096f @JosephPecoraro Initial load of line. Quickly read lines from a file
authored Jan 4, 2009
1 #!/usr/bin/env ruby
2 # Start Date: Sunday January 4, 2009
4e317e5 @JosephPecoraro Added --silent option for more pipe friendly usage.
authored Jan 4, 2009
3 # Current Version: 0.99
022096f @JosephPecoraro Initial load of line. Quickly read lines from a file
authored Jan 5, 2009
4 # Author: Joseph Pecoraro
5 # Contact: joepeck02@gmail.com
6 # Decription: Grab certain lines of a file
4e317e5 @JosephPecoraro Added --silent option for more pipe friendly usage.
authored Jan 5, 2009
7 # with a simple syntax (see usage). Silent
8 # usage useful for piping output.
022096f @JosephPecoraro Initial load of line. Quickly read lines from a file
authored Jan 5, 2009
9
10 # Wrapper around functionality
11 class FileLines
12
13 attr_accessor :lines
14
4e317e5 @JosephPecoraro Added --silent option for more pipe friendly usage.
authored Jan 5, 2009
15 def initialize(filename, options)
022096f @JosephPecoraro Initial load of line. Quickly read lines from a file
authored Jan 5, 2009
16 @lines = File.readlines(filename)
4e317e5 @JosephPecoraro Added --silent option for more pipe friendly usage.
authored Jan 5, 2009
17 @silent = false
18 parse_options(options)
19 end
20
21 def parse_options(options)
22 options.each do |option|
23 if option.match(/^-(-silent|s)$/)
24 @silent = true
25 end
26 end
022096f @JosephPecoraro Initial load of line. Quickly read lines from a file
authored Jan 5, 2009
27 end
28
29 def parse(args)
30 args.each do |arg|
31 if m = arg.match( /^(\d+)-(\d+)/ )
32 lower = m[1].to_i
33 upper = m[2].to_i
34 lower = 1 if lower.zero? # 0 => 1
35 print_lines( lower-1, upper-lower+1 ) if upper >= lower
36 else
37 lineno = arg.to_i
38 lineno = 1 if lineno.zero? # 0 => 1
39 lineno = lineno-1 if lineno > 0 # Handle Negatives
40 print_line( lineno )
41 end
42 puts
43 end
44 end
45
46 def print_lines(lower, upper)
47 i = lower
48 @lines[lower, upper].each do |line|
4e317e5 @JosephPecoraro Added --silent option for more pipe friendly usage.
authored Jan 5, 2009
49 if @silent
50 puts line
51 else
52 puts "[#{i+1}]: #{line}"
53 end
022096f @JosephPecoraro Initial load of line. Quickly read lines from a file
authored Jan 5, 2009
54 i += 1
55 end
56 end
57
58 def print_line(line)
59 real = line>@lines.size ? @lines.size-1 : line
4e317e5 @JosephPecoraro Added --silent option for more pipe friendly usage.
authored Jan 5, 2009
60 if @silent
61 puts @lines[real]
022096f @JosephPecoraro Initial load of line. Quickly read lines from a file
authored Jan 5, 2009
62 else
4e317e5 @JosephPecoraro Added --silent option for more pipe friendly usage.
authored Jan 5, 2009
63 if line < 0
64 puts "[#{real}]: #{@lines[real]}"
65 else
66 puts "[#{real+1}]: #{@lines[real]}"
67 end
022096f @JosephPecoraro Initial load of line. Quickly read lines from a file
authored Jan 5, 2009
68 end
69 end
70
71 end
72
73
74 # When run as as script
75 if $0 == __FILE__
4e317e5 @JosephPecoraro Added --silent option for more pipe friendly usage.
authored Jan 5, 2009
76
77 # Pull out the Options
78 options = ARGV.select { |arg| arg.match(/-[a-z]/i) }
79 ARGV.delete_if { |arg| arg.match(/-[a-z]/i) }
022096f @JosephPecoraro Initial load of line. Quickly read lines from a file
authored Jan 5, 2009
80
81 # Check Cmd Line Args and Print Usage if needed
82 if ARGV.size <= 1
4e317e5 @JosephPecoraro Added --silent option for more pipe friendly usage.
authored Jan 5, 2009
83 puts "usage: line [options] filename numbers"
84 puts
85 puts " options:"
86 puts " --silent or -s Just print the line, without [#]"
022096f @JosephPecoraro Initial load of line. Quickly read lines from a file
authored Jan 5, 2009
87 puts
88 puts " number formats:"
4e317e5 @JosephPecoraro Added --silent option for more pipe friendly usage.
authored Jan 5, 2009
89 puts " 1-3 Prints lines 1 through 3"
90 puts " 5 Prints line 5"
91 puts " -1 Prints the last line"
022096f @JosephPecoraro Initial load of line. Quickly read lines from a file
authored Jan 5, 2009
92 puts
93 exit 1
94
95 # Unreadable File
96 elsif !File.readable?(ARGV[0])
97 puts "line: file '#{ARGV[0]}' did not exist or was unreadable."
98 exit 1
99 end
100
101 # Run the Program
102 puts
4e317e5 @JosephPecoraro Added --silent option for more pipe friendly usage.
authored Jan 5, 2009
103 FileLines.new(ARGV[0], options).parse(ARGV[1, ARGV.size])
022096f @JosephPecoraro Initial load of line. Quickly read lines from a file
authored Jan 5, 2009
104
105 end
Something went wrong with that request. Please try again.