Grep regex to ruby regex

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

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 wrote:


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: 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:] ↩︎

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 "[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:] ↩︎

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:] ↩︎

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:] ↩︎

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!
:smiley:

···

In message “grep regex to ruby regex…” > on 02/07/03, Todd Holloway todd@duckland.org writes:

  					matz.


Paul Duncan pabs@pablotron.org pabs in #gah (OPN IRC)
http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562


  1. [:space:] ↩︎

  2. [:space:] ↩︎

[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!
:smiley:

…right into a sand trap :(.

···


Paul Duncan pabs@pablotron.org pabs in #gah (OPN IRC)
http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562

: > 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!
: :smiley:

Except matz wins because the oringal poster wants it as part of a
ruby program _not_ a one liner ruby program :slight_smile:

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 |
             > http://www.darkrock.co.uk | http://www.ferite.org |

+------------------------------------------------------------------+
"Growing old is mandatory; growing up is optional."