Find a print out a line

Hi
I am slowly learning Ruby and working on a solution. I have huge text
files with tons of garbage and I want to extract just specific lines
including the word 'TestPhase'.

Looking for simple solutions, just struggling with the problem.

Thanks in advance for advice and help.

···

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

Collin Moore wrote:

Hi
I am slowly learning Ruby and working on a solution. I have huge text
files with tons of garbage and I want to extract just specific lines
including the word 'TestPhase'.

Looking for simple solutions, just struggling with the problem.

Thanks in advance for advice and help.

is there a famous one-liner for this ..but its not good for a learning
experience.

This is ok for starters.

File.readlines("file.txt").each do |line|
  puts line if line =~ /TestPhase/
end

have fun
you got homework ..
things to read more about:
File.read* methods
=~ (regular expresions)

···

--
Posted via http://www.ruby-forum.com/\.

If you only need to extract those lines the most efficient solution is probably

fgrep TestPhase <your files>

If you want to do it in Ruby you can do

ruby -ne 'puts $_ if /TestPhase/' <your files>

If you need to further process those lines you can do

ARGF.each |line|
   if /TestPhase/ =~ line
     # ...
     puts line
   end
end

There are probably plenty other options.

Kind regards

  robert

···

On 10/08/2009 10:15 PM, Collin Moore wrote:

I am slowly learning Ruby and working on a solution. I have huge text
files with tons of garbage and I want to extract just specific lines
including the word 'TestPhase'.

Looking for simple solutions, just struggling with the problem.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Collin Moore wrote:

Hi
I am slowly learning Ruby and working on a solution. I have huge text
files with tons of garbage and I want to extract just specific lines
including the word 'TestPhase'.

Looking for simple solutions, just struggling with the problem.

Thanks in advance for advice and help.

$ grep TestPhase *hugefilesglob* > outputFile.txt

Collin Moore schrieb:

Hi
I am slowly learning Ruby and working on a solution. I have huge text
files with tons of garbage and I want to extract just specific lines
including the word 'TestPhase'.

Looking for simple solutions, just struggling with the problem.

Thanks in advance for advice and help.

To reduce the array File.readlines returns to only the lines that contain 'TestPhase' use

File.readlines('file.txt').grep /TestPhase/

how would i write the output to a text file? I tried the following, but
the output displays blank.

I tried a couple File.open attempts, but the output is blank.

···

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

If you don't want to store the whole file in memory while you're grepping
it, then the following will work better.

open('jasmin.txt'){|f| f.each_line.grep /TestPhrase/}

···

On Fri, 09 Oct 2009 21:35:11 +0900, Rüdiger Bahns wrote:

Collin Moore schrieb:

Hi
I am slowly learning Ruby and working on a solution. I have huge text
files with tons of garbage and I want to extract just specific lines
including the word 'TestPhase'.

Looking for simple solutions, just struggling with the problem.

Thanks in advance for advice and help.

To reduce the array File.readlines returns to only the lines that
contain 'TestPhase' use

File.readlines('file.txt').grep /TestPhase/

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

Hi,

···

Am Freitag, 09. Okt 2009, 05:50:04 +0900 schrieb Robert Klemme:

ruby -ne 'puts $_ if /TestPhase/' <your files>

The -p option does the puts:

  ruby -pe '~/TestPhase/ or next' $yourfile

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

how would i write the output to a text file? I tried the following, but
the output displays blank.

For simple ruby scripts I usually have the output go to screen, then
just redirect it to file when I've figured out that it's working.

ruby script.rb > out.txt

or similar.

···

On Thu, Oct 8, 2009 at 10:26 PM, Collin Moore <collin.moore@gmail.com> wrote:

I tried a couple File.open attempts, but the output is blank.
--
Posted via http://www.ruby-forum.com/\.

--
Paul Smith
http://www.nomadicfun.co.uk

paul@pollyandpaul.co.uk

Collin Moore wrote:

how would i write the output to a text file? I tried the following, but
the output displays blank.

I tried a couple File.open attempts, but the output is blank.

fout = File.open("data2.txt", "w")

File.open("data.txt") do |f|
  f.each_line do |line|
    if line.include?("TestPhrase")
      fout.puts(line)
    end
  end
end

fout.close

...or

File.open("results.txt", "w") do |fout|
  IO.foreach("data.txt") do |line|
    if line.include?("TestPhrase")
      fout.puts line
    end
  end
end

If you use blocks with File.open(), then you don't need to manually
close() the file--the block will do it automatically.

···

--
Posted via http://www.ruby-forum.com/\.