[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Searching a file for keywords?

Steve Starr

1/4/2006 3:55:00 PM

I am trying to find out how to search a file and find certain key words
then print them.

Example:

#!/usr/bin/ruby

system("cat /proc/partitions > drives.txt")


Output:
8 0 1007616 sda
8 1 1007600 sda1
3 0 33027622 hda
3 1 14651248 hda1
3 2 14651280 hda2
3 3 3719047 hda3

Now i would just want to print the word hda to the screen.

Any help would be nice.
Thanks

--
Posted via http://www.ruby-....


13 Answers

Robert Klemme

1/4/2006 5:14:00 PM

0

Steve Starr wrote:
> I am trying to find out how to search a file and find certain key
> words then print them.
>
> Example:
>
> #!/usr/bin/ruby
>
> system("cat /proc/partitions > drives.txt")
>
>
> Output:
> 8 0 1007616 sda
> 8 1 1007600 sda1
> 3 0 33027622 hda
> 3 1 14651248 hda1
> 3 2 14651280 hda2
> 3 3 3719047 hda3
>
> Now i would just want to print the word hda to the screen.
>
> Any help would be nice.
> Thanks

ruby -ne 'puts "hda" if /hda/' /proc/partitions

Or, if you just want to match the exact string

ruby -ne 'puts "hda" if /\bhda\b/' /proc/partitions

And if you want to print it once only

ruby -ne '$f ||= /hda/; END { puts "hda" if $f }' /proc/partitions

From within a script

ARGF.select {|l| /hda/ =~ l}.empty? or puts "hda"

HDA - err - HTH

Kind regards

robert

Ross Bamford

1/4/2006 5:21:00 PM

0

On Wed, 04 Jan 2006 15:55:18 -0000, Steve Starr <comtux@gmail.com> wrote:

> I am trying to find out how to search a file and find certain key words
> then print them.
>
> Example:
>
> #!/usr/bin/ruby
>
> system("cat /proc/partitions > drives.txt")
>
>
> Output:
> 8 0 1007616 sda
> 8 1 1007600 sda1
> 3 0 33027622 hda
> 3 1 14651248 hda1
> 3 2 14651280 hda2
> 3 3 3719047 hda3
>
> Now i would just want to print the word hda to the screen.
>
> Any help would be nice.
> Thanks
>

Maybe:

File.open('/proc/partitions') do |f|
f.each_line do |s|
puts s if s =~ /hda/

# Or do you mean:
# s.scan(/hda/) { |it| puts it }
# ?
end
end

Or:

File.open('/proc/partitions') do |f|
puts f.grep(/hda/)
end

Alternatively, here's a use for that wierd gets behaviour people are
talking about, and those hateful Perl-type shortcuts:

$ ruby -ne 'print if ~/hda/' /proc/partitions

(effectively same as)

$ ruby -e 'while gets; print if ~/hda/; end' /proc/partitions

(I assume you already know about the tools that are provided for this
outside Ruby).

--
Ross Bamford - rosco@roscopeco.remove.co.uk

Joel VanderWerf

1/4/2006 5:54:00 PM

0

Ross Bamford wrote:
> File.open('/proc/partitions') do |f|
> puts f.grep(/hda/)
> end

irb(main):008:0> puts IO.read('/proc/partitions').grep(/hda/)
3 0 39070080 hda
3 1 18438808 hda1
3 2 4951800 hda2
3 3 1 hda3
3 5 9855846 hda5
3 6 489951 hda6
3 7 5325516 hda7

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Ross Bamford

1/4/2006 6:26:00 PM

0

On Wed, 04 Jan 2006 17:53:50 -0000, Joel VanderWerf
<vjoel@path.berkeley.edu> wrote:

> Ross Bamford wrote:
>> File.open('/proc/partitions') do |f|
>> puts f.grep(/hda/)
>> end
>
> irb(main):008:0> puts IO.read('/proc/partitions').grep(/hda/)

Ah thanks, that's better.

--
Ross Bamford - rosco@roscopeco.remove.co.uk

Hal E. Fulton

1/4/2006 7:04:00 PM

0

Steve Starr wrote:
> I am trying to find out how to search a file and find certain key words
> then print them.
>
> Example:
>
> #!/usr/bin/ruby
>
> system("cat /proc/partitions > drives.txt")
>
>
> Output:
> 8 0 1007616 sda
> 8 1 1007600 sda1
> 3 0 33027622 hda
> 3 1 14651248 hda1
> 3 2 14651280 hda2
> 3 3 3719047 hda3
>
> Now i would just want to print the word hda to the screen.

Here's a first stab at it:

str = `whatever_command`
arr = str.split(" ")
puts arr.grep(/hda[^0-9]/)

Assuming you just want that word and not the whole line.
Also assuming other things.


Hal



Devin Mullins

1/5/2006 5:15:00 AM

0

Joel VanderWerf wrote:

>puts IO.read('/proc/partitions').grep(/hda/)

puts open('/proc/partions/){|f|f.grep /hda/}

Same number of characters, and much better memory usage for big files, I'd imagine.

Devin

>Ross Bamford wrote:
>
>
>> File.open('/proc/partitions') do |f|
>> puts f.grep(/hda/)
>> end
>>
>>



Joel VanderWerf

1/5/2006 5:04:00 PM

0

Devin Mullins wrote:
> Joel VanderWerf wrote:
>
>> puts IO.read('/proc/partitions').grep(/hda/)
>
>
> puts open('/proc/partions/){|f|f.grep /hda/}
>
> Same number of characters, and much better memory usage for big files,
> I'd imagine.

You're quite right. Using grep on the File rather than on the string
avoids slurping the whole file at once.

(But it's not the same number of chars after you fix the typos! :)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Seth Buttock

1/15/2010 3:16:00 PM

0

On Jan 15, 6:59 am, Garry the Island Boy <garry...@yahoo.com> wrote:


> > Several others said the same exact thing I did, yet you only go after
> > me.  Why are you so obsessed with me Scott?
>
> You're cute? Just guessing. . .
>
> G.

Step away from the Buttock, bro. That smoove talkin' shit might work
on ML but I'm not interested in taking a "cruise" on your "dinghy".
NTTAWWT

mr.rapidan

1/15/2010 4:03:00 PM

0

On Jan 15, 10:56 am, Seth Buttock <calo...@earthlink.net> wrote:
> And you actually make Scott look smart!

Who the hell do you think you are, coming in here and calling Scot
smart?

marcman

1/15/2010 4:08:00 PM

0

On Jan 15, 10:56 am, Seth Buttock <calo...@earthlink.net> wrote:

>
> Her daughter <snip>

I got this far.

You're a dickhead. You read what I had to say, digest it and shit it
out your face if ya like, but tell it to your mama, because I'm done
with you on this.