# Search string in a file

**URL:** <https://rubytalk.org/t/search-string-in-a-file/5842>\
**Category:** ruby-talk\
**Created:** [2 May 2003 01:04 UTC](https://rubytalk.org/t/search-string-in-a-file/5842 "2003-05-02T01:04:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Panther1](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@Panther1](https://rubytalk.org/u/Panther1)\
**Post date:** [2 May 2003 01:04 UTC](https://rubytalk.org/t/search-string-in-a-file/5842/1 "2003-05-02T01:04:09Z")

</div>

I must search string in a file ??  
How I must open file in read and search string ??  
What is syntax ??  
Thamk you

---

<div class="post-metadata">

**Author:** ![Daniel\_Carrera](https://avatars.discourse-cdn.com/v4/letter/d/c67d28/32.png) [@Daniel\_Carrera](https://rubytalk.org/u/Daniel_Carrera)\
**Post date:** [2 May 2003 02:51 UTC](https://rubytalk.org/t/search-string-in-a-file/5842/2 "2003-05-02T02:51:11Z")

</div>

> I must search string in a file ??  
> How I must open file in read and search string ??  
> What is syntax ??  
> Thamk you

Is this what you want?:

dcarrera ~ $ cat \> file  
cat  
dog  
mouse  
eagle  
dcarrera ~ $ irb

> > File.readlines(“file”).each { |line| puts line if line =~ /o/ }  
> > dog  
> > mouse

> **···**
>
> On Fri, May 02, 2003 at 10:04:09AM +0900, Panther wrote:
> 
> > >
> 
> –  
> Daniel Carrera  
> Graduate Teaching Assistant. Math Dept.  
> University of Maryland. (301) 405-5137

---

<div class="post-metadata">

**Author:** ![Chad\_Fowler](https://avatars.discourse-cdn.com/v4/letter/c/5daacb/32.png) [@Chad\_Fowler](https://rubytalk.org/u/Chad_Fowler)\
**Post date:** [15 October 2003 12:24 UTC](https://rubytalk.org/t/search-string-in-a-file/5842/3 "2003-10-15T12:24:23Z")

</div>

# I must search string in a file ??

# How I must open file in read and search string ??

# What is syntax ??

# Thamk you

> **···**
>
> On Wed, 15 Oct 2003, Panther wrote:
> 
> # 
> 
> ruby -ne ‘print if /mystring/’ filename.txt

---

<div class="post-metadata">

**Author:** ![Josef\_Jupp\_SCHUGT](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@Josef\_Jupp\_SCHUGT](https://rubytalk.org/u/Josef_Jupp_SCHUGT)\
**Post date:** [15 October 2003 21:54 UTC](https://rubytalk.org/t/search-string-in-a-file/5842/4 "2003-10-15T21:54:07Z")

</div>

Hi!

- Panther; 2003-10-15, 12:00 UTC:

> I must search string in a file ??  
> How I must open file in read and search string ??  
> What is syntax ??

Two are possible. The following reads file ‘groups’ as a whole and  
then prints out all lines that match /anime/ Regexp:

```
File.readlines("groups").each{ |line|
  print line if line =~ /anime/
}

```

You can alternatively open “groups” and repeat the following until  
EOF has been reached: Read in one line and print it if it matches  
/anime/ Regexp.

```
File.open("groups") { |file|
  while file.gets
    print $_ if /anime/
  end
}

```

HTH

Please take notice of signature! / Bitte Signature beachten!

Josef ‘Jupp’ Schugt

> **···**
>
> –  
> db Wenn sie mir ohne meine Einwilligung geschickt wurde, db  
> dpqb wird eine E-Mail \> 100 kB ungelesen entsorgt dpqb  
> dp::qb If you send me an e-mail \> 100 kB without my dp::qb  
> dp::::qb consent it will be silently discarded dp::::qb

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [2 May 2003 07:59 UTC](https://rubytalk.org/t/search-string-in-a-file/5842/5 "2003-05-02T07:59:02Z")

</div>

Perhaps better not to read the whole file in at once, but process it line at  
a time:

File.open(“file”).each\_line { |line| puts line if line =~ /o/)

If you want to collect the results into an array, then simply

res = File.open(“file”).grep(/o/)

or

res = File.open(“file”) do |f|  
f.grep(/o/)  
end

The second form is a bit longer but has the advantage that the file will be  
closed immediately after reading. The first form won’t do this until  
garbage-collection takes place.

Regards,

Brian.

> **···**
>
> On Fri, May 02, 2003 at 11:51:11AM +0900, Daniel Carrera wrote:
> 
> > On Fri, May 02, 2003 at 10:04:09AM +0900, Panther wrote:
> > 
> > > I must search string in a file ??  
> > > How I must open file in read and search string ??  
> > > What is syntax ??  
> > > Thamk you
> > 
> > Is this what you want?:
> > 
> > dcarrera ~ $ cat \> file  
> > cat  
> > dog  
> > mouse  
> > eagle  
> > dcarrera ~ $ irb
> > 
> > > > File.readlines(“file”).each { |line| puts line if line =~ /o/ }  
> > > > dog  
> > > > mouse

---

<div class="post-metadata">

**Author:** ![Dave\_Baldwin](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@Dave\_Baldwin](https://rubytalk.org/u/Dave_Baldwin)\
**Post date:** [2 May 2003 10:22 UTC](https://rubytalk.org/t/search-string-in-a-file/5842/6 "2003-05-02T10:22:17Z")

</div>

I find these alternative ways of reading and processing a file very  
illuminating and makes me appreciate much more the elegance and power  
or ruby. Would anyone care to add some error handling to them without  
destroying their elegance, as I surely would do.

Thanks,

Dave.

> **···**
>
> On Friday, May 2, 2003, at 08:59 am, Brian Candler wrote:
> 
> > On Fri, May 02, 2003 at 11:51:11AM +0900, Daniel Carrera wrote:
> > 
> > > On Fri, May 02, 2003 at 10:04:09AM +0900, Panther wrote:
> > > 
> > > > I must search string in a file ??  
> > > > How I must open file in read and search string ??  
> > > > What is syntax ??  
> > > > Thamk you
> > > 
> > > Is this what you want?:
> > > 
> > > dcarrera ~ $ cat \> file  
> > > cat  
> > > dog  
> > > mouse  
> > > eagle  
> > > dcarrera ~ $ irb
> > > 
> > > > > File.readlines(“file”).each { |line| puts line if line =~ /o/ }  
> > > > > dog  
> > > > > mouse
> > 
> > Perhaps better not to read the whole file in at once, but process it  
> > line at  
> > a time:
> > 
> > File.open(“file”).each\_line { |line| puts line if line =~ /o/)
> > 
> > If you want to collect the results into an array, then simply
> > 
> > res = File.open(“file”).grep(/o/)
> > 
> > or
> > 
> > res = File.open(“file”) do |f|  
> > f.grep(/o/)  
> > end
> > 
> > The second form is a bit longer but has the advantage that the file  
> > will be  
> > closed immediately after reading. The first form won’t do this until  
> > garbage-collection takes place.
> > 
> > Regards,
> > 
> > Brian.

---

<div class="post-metadata">

**Author:** ![David\_A\_Black2](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@David\_A\_Black2](https://rubytalk.org/u/David_A_Black2)\
**Post date:** [2 May 2003 10:46 UTC](https://rubytalk.org/t/search-string-in-a-file/5842/7 "2003-05-02T10:46:04Z")

</div>

Hi –

> I find these alternative ways of reading and processing a file very  
> illuminating and makes me appreciate much more the elegance and power  
> or ruby. Would anyone care to add some error handling to them without  
> destroying their elegance, as I surely would do.

With trepidation… 🙂

You can always wrap them in a rescue clause:

begin  
puts File.open(“file”) {|f| f.grep(/o/)}  
rescue =\> e  
puts “Problem with IO operation: #{e}”  
exit 1  
end

David

> **···**
>
> On Fri, 2 May 2003, Dave Baldwin wrote:
> 
> –  
> David Alan Black  
> home: [dblack@superlink.net](mailto:dblack@superlink.net)  
> work: blackdav@shu.edu  
> Web: [http://pirate.shu.edu/~blackdav](http://pirate.shu.edu/~blackdav)
