Skip to content

Commit

Permalink
add i for line-number
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Apr 16, 2011
1 parent 33c8238 commit 59baa52
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ A few simple examples.<br/>
ls -al | grep foo
ls -al | pru /foo/

# grep --- all lines including current date
ls -al | ???
ls -al | pru 'include?(Time.now.strftime("%Y-%m-%d"))'

# grep --- all lines including foo but not self
ps -ef | grep foo | grep -v grep
ps -ef | pru 'include?("foo") and not include?("pru")'
Expand All @@ -44,6 +48,9 @@ A few simple examples.<br/>
ls -al | sed 's/5/five/'
ls -al | pru 'gsub(/5/,"five")'

# every second line
ls -al | pru 'i % 2 == 0'


Author
======
Expand Down
2 changes: 2 additions & 0 deletions lib/pru.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Pru
VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip

def self.map(io, code)
i = 1
io.readlines.each do |line|
result = line.instance_exec{ eval(code) }
if result == true
Expand All @@ -11,6 +12,7 @@ def self.map(io, code)
elsif result
yield result
end
i += 1
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/pru_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
`ls -l | ./bin/pru /G/`.split("\n").size.should == 2
end

it "can selects via i" do
`ls -l | ./bin/pru 'i'`.split("\n")[0...3].should == ["1","2","3"]
end

it "maps" do
`echo abc | ./bin/pru 'gsub(/a/,"b")'`.should == "bbc\n"
end
Expand Down

0 comments on commit 59baa52

Please sign in to comment.