# Grep regex to ruby regex

**URL:** <https://rubytalk.org/t/grep-regex-to-ruby-regex/483>\
**Category:** ruby-talk\
**Created:** [2 July 2002 16:34 UTC](https://rubytalk.org/t/grep-regex-to-ruby-regex/483 "2002-07-02T16:34:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Todd\_Holloway](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@Todd\_Holloway](https://rubytalk.org/u/Todd_Holloway)\
**Post date:** [2 July 2002 16:34 UTC](https://rubytalk.org/t/grep-regex-to-ruby-regex/483/1 "2002-07-02T16:34:07Z")

</div>

I’m just about to finish up my first ruby program, but I’m  
stuck on one last part…I need equivlent of the below in ruby:

grep -v "^[[:space:]]_#" filename | grep -v '^[[:space:]]_$’

thanks in advance! I will send out final program once it’s  
done.

todd

> **···**
>
> –  
> “This UI has been brought to you by the letters ‘S’ and ‘K’, and the runlevel 3.”  
> - Greg Andrews

---

<div class="post-metadata">

**Author:** ![Dossy](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@Dossy](https://rubytalk.org/u/Dossy)\
**Post date:** [2 July 2002 17:02 UTC](https://rubytalk.org/t/grep-regex-to-ruby-regex/483/2 "2002-07-02T17:02:54Z")

</div>

> I’m just about to finish up my first ruby program, but I’m  
> stuck on one last part…I need equivlent of the below in ruby:
> 
> grep -v "\[1\]_#" filename | grep -v '\[2\]_$’
> 
> thanks in advance! I will send out final program once it’s  
> done.

$ cat \<\<-EOT \>foo.rb  
filename = ARGV[0]

# this is a test

File.open(filename) {  
\>file\>  
# some more test  
file.each\_line {  
\>line\> # this shouldn’t get removed  
next if line =~ /^\s\*#/  
next if line =~ /^\s\*$/  
puts line  
}  
}  
EOT

$ ruby foo.rb foo.rb  
filename = ARGV[0]  
File.open(filename) {  
\>file\>  
file.each\_line {  
\>line\> # this shouldn’t get removed  
next if line =~ /^\s\*#/  
next if line =~ /^\s\*$/  
puts line  
}  
}

$

That help?

– Dossy

> **···**
>
> On 2002.07.03, Todd Holloway [todd@duckland.org](mailto:todd@duckland.org) wrote:
> 
> –  
> Dossy Shiobara mail: [dossy@panoptic.com](mailto:dossy@panoptic.com)  
> Panoptic Computer Network web: [http://www.panoptic.com/](http://www.panoptic.com/)  
> “He realized the fastest way to change is to laugh at your own  
> folly – then you can let go and quickly move on.” (p. 70)

* * *

1. [:space:] 

2. [:space:]

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [2 July 2002 17:15 UTC](https://rubytalk.org/t/grep-regex-to-ruby-regex/483/3 "2002-07-02T17:15:42Z")

</div>

Hi,

> **···**
>
> In message “grep regex to ruby regex…” on 02/07/03, Todd Holloway [todd@duckland.org](mailto:todd@duckland.org) writes:
> 
> > I’m just about to finish up my first ruby program, but I’m  
> > stuck on one last part…I need equivlent of the below in ruby:
> > 
> > grep -v "\[1\]_#" filename | grep -v '\[2\]_$’
> 
> File.foreach(filename) do |line|  
> next if /^\s\*#/ =~ line  
> next if /^\s\*$/ =~ line  
> print line  
> end
> 
> ```
> matz.
> 
> ```

* * *

1. [:space:] 

2. [:space:]

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [2 July 2002 17:38 UTC](https://rubytalk.org/t/grep-regex-to-ruby-regex/483/4 "2002-07-02T17:38:53Z")

</div>

Hi,

> **···**
>
> At Wed, 3 Jul 2002 01:34:07 +0900, Todd Holloway wrote:
> 
> > grep -v "\[1\]_#" filename | grep -v '\[2\]_$’
> 
> ruby -pe ‘$\_ = “” if /\[3\]_(?:#|$)/’  
> ruby -ne 'print unless /\[4\]_(?:#|$)/’
> 
> –  
> Nobu Nakada

* * *

1. [:space:] 

2. [:space:] 

3. [:space:] 

4. [:space:]

---

<div class="post-metadata">

**Author:** ![SER1](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@SER1](https://rubytalk.org/u/SER1)\
**Post date:** [2 July 2002 20:12 UTC](https://rubytalk.org/t/grep-regex-to-ruby-regex/483/5 "2002-07-02T20:12:22Z")

</div>

Todd Holloway wrote:

> stuck on one last part…I need equivlent of the below in ruby:
> 
> grep -v "\[1\]_#" filename | grep -v '\[2\]_$’

ruby -nle ‘print unless /^\s\*(#.\*|)$/’ filename

> **···**
>
> –
> 
> > … “Paradise is exactly like where you are right now … only much,  
> > \<|\> much better.”  
> > /|\ – Laurie Anderson  
> > /|

* * *

1. [:space:] 

2. [:space:]

---

<div class="post-metadata">

**Author:** ![Paul\_Duncan](https://avatars.discourse-cdn.com/v4/letter/p/b487fb/32.png) [@Paul\_Duncan](https://rubytalk.org/u/Paul_Duncan)\
**Post date:** [2 July 2002 19:35 UTC](https://rubytalk.org/t/grep-regex-to-ruby-regex/483/6 "2002-07-02T19:35:13Z")

</div>

> Hi,
> 
> > I’m just about to finish up my first ruby program, but I’m  
> > stuck on one last part…I need equivlent of the below in ruby:
> > 
> > grep -v "\[1\]_#" filename | grep -v '\[2\]_$’
> 
> File.foreach(filename) do |line|  
> next if /^\s\*#/ =~ line  
> next if /^\s\*$/ =~ line  
> print line  
> end

ruby -lne ‘print unless /\s\*#/ or !/\S/’ filename

I’m golfing with the champ!  
😃

> **···**
>
> - Yukihiro Matsumoto ([matz@ruby-lang.org](mailto:matz@ruby-lang.org)) wrote:
> 
> > In message “grep regex to ruby regex…” \> on 02/07/03, Todd Holloway [todd@duckland.org](mailto:todd@duckland.org) writes:
> 
> > ```
> > matz.
> > 
> > ```
> 
> –  
> Paul Duncan [pabs@pablotron.org](mailto:pabs@pablotron.org) pabs in #gah (OPN IRC)  
> [http://www.pablotron.org/](http://www.pablotron.org/) OpenPGP Key ID: 0x82C29562

* * *

1. [:space:] 

2. [:space:]

---

<div class="post-metadata">

**Author:** ![Paul\_Duncan](https://avatars.discourse-cdn.com/v4/letter/p/b487fb/32.png) [@Paul\_Duncan](https://rubytalk.org/u/Paul_Duncan)\
**Post date:** [2 July 2002 19:48 UTC](https://rubytalk.org/t/grep-regex-to-ruby-regex/483/7 "2002-07-02T19:48:26Z")

</div>

[snipped]

> ruby -lne ‘print unless /\s\*#/ or !/\S/’ filename

Of course i forgot a carat; it should be “/^\s\*#/”, as dblack pointed  
out in #ruby-lang.

> I’m golfing with the champ!  
> 😃

…right into a sand trap :(.

> **···**
>
> - Paul Duncan ([pabs@pablotron.org](mailto:pabs@pablotron.org)) wrote:
> 
> –  
> Paul Duncan [pabs@pablotron.org](mailto:pabs@pablotron.org) pabs in #gah (OPN IRC)  
> [http://www.pablotron.org/](http://www.pablotron.org/) OpenPGP Key ID: 0x82C29562

---

<div class="post-metadata">

**Author:** ![Chris\_Ross](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@Chris\_Ross](https://rubytalk.org/u/Chris_Ross)\
**Post date:** [2 July 2002 21:45 UTC](https://rubytalk.org/t/grep-regex-to-ruby-regex/483/8 "2002-07-02T21:45:00Z")

</div>

: \> Hi,  
: \>  
: \> In message "grep regex to ruby regex..."  
: \> on 02/07/03, Todd Holloway \<todd@duckland.org\> writes:  
: \>  
: \> |I'm just about to finish up my first ruby program, but I'm  
: \> |stuck on one last part...I need equivlent of the below in ruby:  
: \> |  
: \> |grep -v "^[[:space:]]\*#" filename | grep -v '^[[:space:]]\*$'  
: \>  
: \> File.foreach(filename) do |line|  
: \> next if /^\s\*#/ =~ line  
: \> next if /^\s\*$/ =~ line  
: \> print line  
: \> end  
:  
: ruby -lne 'print unless /\s\*#/ or !/\S/' filename  
:  
: I'm golfing with the champ!  
: 😃

Except matz wins because the oringal poster wants it as part of a  
ruby program \_not\_ a one liner ruby program 🙂

Regards,

Chris

> **···**
>
> On Tue, 02 Jul 2002 20:35:13 Paul Duncan wrote:  
> : \* Yukihiro Matsumoto (matz@ruby-lang.org) wrote:  
> --  
> +------------------------------------------------------------------+
> 
> > Chris Ross | chris@darkrock.co.uk | ctr@ferite.org |  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> [http://www.darkrock.co.uk](http://www.darkrock.co.uk) | [http://www.ferite.org](http://www.ferite.org) |
> 
> +------------------------------------------------------------------+  
> "Growing old is mandatory; growing up is optional."
