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.

Collin M. 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)

On 10/08/2009 10:15 PM, Collin M. 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.

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

fgrep TestPhase

If you want to do it in Ruby you can do

ruby -ne ‘puts $_ if /TestPhase/’

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

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.

On Thu, Oct 8, 2009 at 10:26 PM, Collin M. [email protected]
wrote:

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.

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

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


Paul S.
http://www.nomadicfun.co.uk

[email protected]

Collin M. 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 M. 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/

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

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

File.readlines(‘file.txt’).grep /TestPhase/

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/}

Hi,

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

ruby -ne ‘puts $_ if /TestPhase/’

The -p option does the puts:

ruby -pe ‘~/TestPhase/ or next’ $yourfile

Bertram

Collin M. 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs