[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Simple Regex help

Its Me

1/13/2008 1:38:00 AM

I have a file of code that has "!"-delimited lines and other non !-delimited
lines. I want to remove all the "!"-delimited lines

!some stuff that i do not want!

lots of other stuff that i want
lots of other stuff that i want

!some stuff that i do not want!

lots of other stuff that i want
lots of other stuff that i want

What would be a simple ruby script to use regex and remove all the
!....!
lines?

Thanks!


7 Answers

Phrogz

1/13/2008 2:11:00 AM

0

On Jan 12, 6:37 pm, "itsme213" <itsme...@hotmail.com> wrote:
> I have a file of code that has "!"-delimited lines and other non !-delimited
> lines. I want to remove all the "!"-delimited lines
>
> !some stuff that i do not want!
>
> lots of other stuff that i want
> lots of other stuff that i want
>
> !some stuff that i do not want!
>
> lots of other stuff that i want
> lots of other stuff that i want
>
> What would be a simple ruby script to use regex and remove all the
>   !....!

IO.readlines( 'file.txt' ).delete_if{ |line| line =~ /^!.*!$/ }
#=> returns an array; you may want to .join it with the newline
seperator.

Its Me

1/13/2008 3:55:00 AM

0

Thanks!

"Phrogz" <phrogz@mac.com> wrote
>> What would be a simple ruby script to use regex and remove all the
>> !....!

>IO.readlines( 'file.txt' ).delete_if{ |line| line =~ /^!.*!$/ }
>#=> returns an array; you may want to .join it with the newline
>seperator.


Its Me

1/13/2008 5:10:00 AM

0

"itsme213" <itsme213@hotmail.com> wrote

>>IO.readlines( 'file.txt' ).delete_if{ |line| line =~ /^!.*!$/ }

Oops, spoke too soon. The file seems to have just '\r' at the end of each
line (Unix vs. Windows?).

IO.readlines(myfile)[0] gives
#=>"good stuff\r!badstuff!\rMore good stuff\r!More bad stuff!\r"

I tried some alternate regexes using \r but am pretty hopeless at it :-(

Thanks.


William James

1/13/2008 8:12:00 AM

0

On Jan 12, 7:37 pm, "itsme213" <itsme...@hotmail.com> wrote:
> I have a file of code that has "!"-delimited lines and other non !-delimited
> lines. I want to remove all the "!"-delimited lines
>
> !some stuff that i do not want!
>
> lots of other stuff that i want
> lots of other stuff that i want
>
> !some stuff that i do not want!
>
> lots of other stuff that i want
> lots of other stuff that i want
>
> What would be a simple ruby script to use regex and remove all the
> !....!
> lines?
>
> Thanks!

awk "!/!/" myfile >newfile

ruby -pne "next if /!/" myfile >newfile


> Oops, spoke too soon. The file seems to have just '\r' at the end
> of each line (Unix vs. Windows?).
>
> IO.readlines(myfile)[0] gives
> #=>"good stuff\r!badstuff!\rMore good stuff\r!More bad stuff!\r"

ruby -pne "BEGIN{$/=13.chr}; next if /!/" myfile >newfile

awk "!/!/" RS="\r" myfile >newfile

Phrogz

1/13/2008 3:19:00 PM

0

On Jan 13, 1:12 am, William James <w_a_x_...@yahoo.com> wrote:
> On Jan 12, 7:37 pm, "itsme213" <itsme...@hotmail.com> wrote:
>
>
>
> > I have a file of code that has "!"-delimited lines and other non !-delimited
> > lines. I want to remove all the "!"-delimited lines
>
> > !some stuff that i do not want!
>
> > lots of other stuff that i want
> > lots of other stuff that i want
>
> > !some stuff that i do not want!
>
> > lots of other stuff that i want
> > lots of other stuff that i want
>
> > What would be a simple ruby script to use regex and remove all the
> >   !....!
> > lines?
>
> > Thanks!
>
> awk "!/!/" myfile >newfile
>
> ruby -pne "next if /!/" myfile >newfile
>
> > Oops, spoke too soon. The file seems to have just '\r' at the end
> > of each line (Unix vs. Windows?).
>
> > IO.readlines(myfile)[0] gives
> > #=>"good stuff\r!badstuff!\rMore good stuff\r!More bad stuff!\r"
>
> ruby -pne "BEGIN{$/=13.chr}; next if /!/" myfile >newfile

The trick here is setting $/ to the value you want. For more
information, see:
http://phrogz.net/ProgrammingRuby/language.html#inputoutpu...

Phrogz

1/13/2008 3:23:00 PM

0

On Jan 12, 10:09 pm, "itsme213" <itsme...@hotmail.com> wrote:
> Oops, spoke too soon. The file seems to have just '\r' at the end of each
> line (Unix vs. Windows?).

Files with \n only as the line separator are produced by Mac OS X, and
other unix/linux/*nix OS applications.

Files with \r\n as the line separator are produced by Windows.

Files with \r only as the line separator are produced by MacOS 9
applications, or confused programs/programmers.

I'm surprised that you have a file with really just \r.

Its Me

1/13/2008 6:36:00 PM

0

"Phrogz" <phrogz@mac.com> wrote

> The trick here is setting $/ to the value you want. For more
> information, see:
> http://phrogz.net/ProgrammingRuby/language.html#inputoutpu...

Ah, thank you. Btw, the \r is in a Smalltalk file-out file.