From: Peter Vandenabeele Date: 2012-01-14T03:01:33+09:00 Subject: Re: negative grep --20cf300faee397d67404b66ca7f2 Content-Type: text/plain; charset=UTF-8 On Fri, Jan 13, 2012 at 3:18 PM, Robert Klemme wrote: > On Thu, Jan 12, 2012 at 10:22 PM, Peter Vandenabeele > wrote: > ... > > > > Well, a 'v' option, equivalent to the UNIX grep seems quite > straightforward > > to conceive? > > > > peterv@ASUS:~$ ruby -v > > ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux] > > peterv@ASUS:~$ echo -e "abc\ndef\nghi" | grep abc > > abc > > peterv@ASUS:~$ echo -e "abc\ndef\nghi" | grep -v abc > > def > > ghi > > peterv@ASUS:~$ ruby -e 'puts %w{ abc def ghi}.grep(/abc/)' > > abc > > peterv@ASUS:~$ ruby -e 'puts %w{ abc def ghi}.grep(/abc/v)' > > -e:1: unknown regexp option - v > > > > Having "def" and "ghi" as a result here would be nice :-) > > Pretty easy to do: > > module Enumerable > def grep_v(cond) > select {|x| not cond === x} > end > end > > irb(main):006:0> a=10.times.map &:to_s > => ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] > irb(main):007:0> a.grep /0/ > => ["0"] > irb(main):008:0> a.grep_v /0/ > => ["1", "2", "3", "4", "5", "6", "7", "8", "9"] > > Thank you. It took me quite some time to grasp that the '-i' and '-v' options of $ grep -i $ grep -v do something very different ... '-i' changes the behavior of the Regexp _itself_ '-v' changes the behavior of the filter on the lines, _after_ the regexp is calculated (changes it from "ruby select" to "ruby reject") So, I believe now that my earlier proposal of a Ruby regexp like /.../v does not make a lot of sense in this context. Negation could be an option for the Enumerable grep command, but that is probably more complex than just implementing the grep_v you suggested. Thanks, Peter --20cf300faee397d67404b66ca7f2--