Grep regex to ruby regex


(Todd Holloway) #1

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


(Dossy) #2

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.

$ 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)


(Yukihiro Matsumoto) #3

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

						matz.

(Nobuyoshi Nakada) #4

Hi,

···

At Wed, 3 Jul 2002 01:34:07 +0900, Todd Holloway wrote:

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

ruby -pe ‘$_ = “” if /^[[:space:]](?:#|$)/'
ruby -ne 'print unless /^[[:space:]]
(?:#|$)/’


Nobu Nakada


(SER) #5

Todd Holloway wrote:

stuck on one last part…I need equivlent of the below in ruby:

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

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

···

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


(Paul Duncan) #6

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 "^[[: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:

···

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


(Paul Duncan) #7

[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


(Chris Ross) #8

: > 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."