jan
1
7
Updates to line.rb – Open to more suggestions
I was chatting with a friend the other day and he had some really good ideas concerning last week’s line.rb script. He wants to make an improved version. I look forward to seeing what he will come up with because he shared some great ideas. Well, I couldn’t help but implement them immediately in my version. His ideas also spurred a little creativity from me and I came up with a few new features as well. The usage is now looks like:
# Check Cmd Line Args and Print Usage if needed
if ARGV.size <= 1
puts "usage: line [options] filename numbers"
puts
puts " options:"
puts " --silent or -s Just print the line, without [#]"
puts
puts " number formats:"
puts " 1-3 Prints lines 1 through 3"
puts " 5 Prints line 5"
puts " -1 Prints the last line, (negative)"
puts
puts " extra formats:"
puts " ~5 Prints 2 (default) lines before and after 5"
puts " 4~10 Prints 4 lines before and after 10"
puts " *7 or 8* Prints all lines before or after the number"
puts " 5/1 Prints 5 lines, then skips 1 line..."
puts " 2:5/1 Starts at line 2, prints and skips..."
puts
exit 1
So there is some cool new syntax:
-
~# – prints 2 lines before and after the given line number
-
#~# – prints the given number of lines before and after the given line number
-
*# – prints all of the lines before the given line number
-
#* – prints all of the lines after the given line number
-
#/# – is a print and skip option
-
#:#/# – allows you to provide an offset
Lets say you want to print every even line in a text file. With this script that is not a problem. The solution is: starting on the 2nd line, print 1 line, then skip 1 line. (line file 2:1/1):
I think these new formats are pretty neat. You can always access the latest version of the script on Github or in my ~/bin. I’m willing to change the syntax and introduce new concepts. Send suggestions my way by dropping a comment! Cheers.