Search string in a file

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

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

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

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

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

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.

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… :slight_smile:

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
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav