Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > A method to search and cut in Ruby.

Reply
Thread Tools

A method to search and cut in Ruby.

 
 
scomboni@gmail.com
Guest
Posts: n/a
 
      07-25-2007
Hello all,
I'm trying to parse some EXIF data and return certain fields of text.
If I wanted to parsed a document looking for text and then locating it
take everything after a colon : delimitator what is the best method in
ruby vs Shell command?

For example if I have a file called filename.txt and I want to search
for this ---> "Compression : JPEG (old-style)"
but only return everything to the right of the delimitator?

Using command line tool such as grep to locate and then cut to grab
everything after " : " works but I'm trying to learn how to do things
like this in Ruby and having no luck grabbing everything to the right
of the colon. Some of the text I'm grabbing can contain additional
colons and I want everything after the first one.


Thanks.

Sc--

 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      07-25-2007
On Jul 25, 4:59 pm, scomb...@gmail.com wrote:
> For example if I have a file called filename.txt and I want to search
> for this ---> "Compression : JPEG (old-style)"
> but only return everything to the right of the delimitator?


C:\>irb
irb(main):001:0> s = "Foo : Bar"
=> "Foo : Bar"
irb(main):002:0> s.split ":"
=> ["Foo ", " Bar"]
irb(main):003:0> s.split(":").last
=> " Bar"

....or did you want the leading whitespace chomped?

irb(main):004:0> s.split( /\s*:\s*/ )
=> ["Foo", "Bar"]
irb(main):005:0> s.split( /\s*:\s*/ ).last
=> "Bar"

 
Reply With Quote
 
 
 
 
scomboni@gmail.com
Guest
Posts: n/a
 
      07-26-2007
On Jul 25, 7:05 pm, Phrogz <phr...@mac.com> wrote:
> On Jul 25, 4:59 pm, scomb...@gmail.com wrote:
>
> > For example if I have a file called filename.txt and I want to search
> > for this ---> "Compression : JPEG (old-style)"
> > but only return everything to the right of the delimitator?

>
> C:\>irb
> irb(main):001:0> s = "Foo : Bar"
> => "Foo : Bar"
> irb(main):002:0> s.split ":"
> => ["Foo ", " Bar"]
> irb(main):003:0> s.split(":").last
> => " Bar"
>
> ...or did you want the leading whitespace chomped?
>
> irb(main):004:0> s.split( /\s*:\s*/ )
> => ["Foo", "Bar"]
> irb(main):005:0> s.split( /\s*:\s*/ ).last
> => "Bar"


I was looking at this method as well. I was having trouble though as I
know the left hand side column so for this example the Compression
portion then the colon : the remainder could be anything for instance
a description with lots of text and could also contain a additional
colon. At the shell prompt I could grep "Compression" | cut -d: -f2-
and that would grab everything...
So it sounds like I'm looking in the correct area but Im just not
pulling it all together. I will keep at it if you have additional info
to point to that would be great..and appreciated.
Thanks again.


Sc-


 
Reply With Quote
 
bbiker
Guest
Posts: n/a
 
      07-26-2007
On Jul 25, 7:05 pm, Phrogz <phr...@mac.com> wrote:
> On Jul 25, 4:59 pm, scomb...@gmail.com wrote:
>
> > For example if I have a file called filename.txt and I want to search
> > for this ---> "Compression : JPEG (old-style)"
> > but only return everything to the right of the delimitator?

>
> C:\>irb
> irb(main):001:0> s = "Foo : Bar"
> => "Foo : Bar"
> irb(main):002:0> s.split ":"
> => ["Foo ", " Bar"]
> irb(main):003:0> s.split(":").last
> => " Bar"
>
> ...or did you want the leading whitespace chomped?
>
> irb(main):004:0> s.split( /\s*:\s*/ )
> => ["Foo", "Bar"]
> irb(main):005:0> s.split( /\s*:\s*/ ).last
> => "Bar"



Some of the text I'm grabbing can contain additional
colons and I want everything after the first one.

SC
I am assuming you want everything after the first colon but not after
the second

irb(main):001:0> line = "Compression : JPEG (old_style) : whatever"
=> "Compression : JPEG (old_style) : whatever"
irb(main):002:0> if line =~ /^Compression\s*(.*)$/
irb(main):003:1> end_str = $1
irb(main):004:1> first_el = end_str.split(/:/).first
puts first_el
irb(main):005:1> end
JPEG (old_style)

if you everything after each colon

el = end_str.split(/:/)

p el = {"JPEG (old-style)", "whatever"]

 
Reply With Quote
 
Phil Meier
Guest
Posts: n/a
 
      07-26-2007
schrieb:
> On Jul 25, 7:05 pm, Phrogz <phr...@mac.com> wrote:
>> On Jul 25, 4:59 pm, scomb...@gmail.com wrote:

....
>> ...or did you want the leading whitespace chomped?
>>
>> irb(main):004:0> s.split( /\s*:\s*/ )
>> => ["Foo", "Bar"]
>> irb(main):005:0> s.split( /\s*:\s*/ ).last
>> => "Bar"

>
> I was looking at this method as well. I was having trouble though as I
> know the left hand side column so for this example the Compression
> portion then the colon : the remainder could be anything for instance
> a description with lots of text and could also contain a additional
> colon. At the shell prompt I could grep "Compression" | cut -d: -f2-
> and that would grab everything...


To get everything after the first colon you can use:
s =~ /^.*?:/
textAfterColon = $'.dup

To also get rid of the spaces directly after the first colon use this Regex:
s =~ /^.*?:\s*/
textAfterColon = $'.dup

The trick is to anchor the regex (i.e. using ^)

BR Phil
 
Reply With Quote
 
scomboni@gmail.com
Guest
Posts: n/a
 
      07-26-2007
On Jul 26, 3:28 am, Phil Meier <phil.me...@gmx.net> wrote:
> scomb...@gmail.com schrieb:
>
>
>
> > On Jul 25, 7:05 pm, Phrogz <phr...@mac.com> wrote:
> >> On Jul 25, 4:59 pm, scomb...@gmail.com wrote:

> ...
> >> ...or did you want the leading whitespace chomped?

>
> >> irb(main):004:0> s.split( /\s*:\s*/ )
> >> => ["Foo", "Bar"]
> >> irb(main):005:0> s.split( /\s*:\s*/ ).last
> >> => "Bar"

>
> > I was looking at this method as well. I was having trouble though as I
> > know the left hand side column so for this example the Compression
> > portion then the colon : the remainder could be anything for instance
> > a description with lots of text and could also contain a additional
> > colon. At the shell prompt I could grep "Compression" | cut -d: -f2-
> > and that would grab everything...

>
> To get everything after the first colon you can use:
> s =~ /^.*?:/
> textAfterColon = $'.dup
>
> To also get rid of the spaces directly after the first colon use this Regex:
> s =~ /^.*?:\s*/
> textAfterColon = $'.dup
>
> The trick is to anchor the regex (i.e. using ^)
>
> BR Phil


Thanks so much for everyones answers much appreciated. The space after
the colon, I thought I was going to have to live with that. Thanks
again for the help.
Scott

 
Reply With Quote
 
Thomas Gantner
Guest
Posts: n/a
 
      07-26-2007
on Thu 26. July 2007 02.37, wrote:

> On Jul 25, 7:05 pm, Phrogz <phr...@mac.com> wrote:
>> On Jul 25, 4:59 pm, scomb...@gmail.com wrote:
>>
>> > For example if I have a file called filename.txt and I want to search
>> > for this ---> "Compression : JPEG (old-style)"
>> > but only return everything to the right of the delimitator?

>>
>> C:\>irb
>> irb(main):001:0> s = "Foo : Bar"
>> => "Foo : Bar"
>> irb(main):002:0> s.split ":"
>> => ["Foo ", " Bar"]
>> irb(main):003:0> s.split(":").last
>> => " Bar"
>>
>> ...or did you want the leading whitespace chomped?
>>
>> irb(main):004:0> s.split( /\s*:\s*/ )
>> => ["Foo", "Bar"]
>> irb(main):005:0> s.split( /\s*:\s*/ ).last
>> => "Bar"

>
> I was looking at this method as well. I was having trouble though as I
> know the left hand side column so for this example the Compression
> portion then the colon : the remainder could be anything for instance
> a description with lots of text and could also contain a additional
> colon. At the shell prompt I could grep "Compression" | cut -d: -f2-
> and that would grab everything...
> So it sounds like I'm looking in the correct area but Im just not
> pulling it all together. I will keep at it if you have additional info
> to point to that would be great..and appreciated.
> Thanks again.


split takes an optional second parameter <limit>, defining how many elements
the resuting array shall have at most

'foo : bar : baz'.split(/\s*:\s*/, 2)
=> [ "foo", "bar : baz" ]

-Thomas

--
<sig. under construction>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
| SEO , Search Engine Optimizer, SEARCH OPtiMIzAtIoN with SeaRch OPtiMizer optimizer.seo@gmail.com Digital Photography 0 04-22-2007 04:20 AM
short cut to do a inspect method for object junkone@rogers.com Ruby 1 07-24-2006 03:11 AM
search within a search within a search - looking for better way...my script times out Abby Lee ASP General 5 08-02-2004 04:01 PM
removing "favorites" and how to "cut and paste" shirley Microsoft Certification 1 09-26-2003 10:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57