# Searching a file for keywords?

**URL:** <https://www.ruby-forum.com/t/searching-a-file-for-keywords/89382>\
**Category:** Ruby\
**Created:** [January 4, 2006, 4:55pm UTC](https://www.ruby-forum.com/t/searching-a-file-for-keywords/89382 "2006-01-04T16:55:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![comtux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@comtux](https://www.ruby-forum.com/u/comtux)\
**Post date:** [January 4, 2006, 4:55pm UTC](https://www.ruby-forum.com/t/searching-a-file-for-keywords/89382/1 "2006-01-04T16:55:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![comtux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@comtux](https://www.ruby-forum.com/u/comtux)\
**Post date:** [January 4, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/searching-a-file-for-keywords/89382/2 "2006-01-04T18:20:39Z")

</div>

Steve S. wrote:

> Output:  
> 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
```

---

<div class="post-metadata">

**Author:** ![comtux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@comtux](https://www.ruby-forum.com/u/comtux)\
**Post date:** [January 4, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/searching-a-file-for-keywords/89382/3 "2006-01-04T18:29:52Z")

</div>

On Wed, 04 Jan 2006 15:55:18 -0000, Steve S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Output:  
> 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).

---

<div class="post-metadata">

**Author:** ![comtux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@comtux](https://www.ruby-forum.com/u/comtux)\
**Post date:** [January 4, 2006, 7:36pm UTC](https://www.ruby-forum.com/t/searching-a-file-for-keywords/89382/4 "2006-01-04T19:36:13Z")

</div>

On Wed, 04 Jan 2006 17:53:50 -0000, Joel VanderWerf  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ross B. 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.

---

<div class="post-metadata">

**Author:** ![comtux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@comtux](https://www.ruby-forum.com/u/comtux)\
**Post date:** [January 4, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/searching-a-file-for-keywords/89382/5 "2006-01-04T18:53:58Z")

</div>

Ross B. 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

---

<div class="post-metadata">

**Author:** ![comtux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@comtux](https://www.ruby-forum.com/u/comtux)\
**Post date:** [January 4, 2006, 8:06pm UTC](https://www.ruby-forum.com/t/searching-a-file-for-keywords/89382/6 "2006-01-04T20:06:24Z")

</div>

Steve S. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![comtux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@comtux](https://www.ruby-forum.com/u/comtux)\
**Post date:** [January 5, 2006, 6:16am UTC](https://www.ruby-forum.com/t/searching-a-file-for-keywords/89382/7 "2006-01-05T06:16:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![comtux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@comtux](https://www.ruby-forum.com/u/comtux)\
**Post date:** [January 5, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/searching-a-file-for-keywords/89382/8 "2006-01-05T19:57:29Z")

</div>

Devin M. 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! 🙂
