find a print out a line

C

Collin Moore

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.
 
R

Rodrigo Bermejo

Collin said:
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)
 
R

Robert Klemme

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 <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
 
C

Collin Moore

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.
 
P

Paul Smith

how would i write the output to a text file? =A0I tried the following, bu= t
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.



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

(e-mail address removed)
 
R

Reid Thompson

Collin said:
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
 
R

Rüdiger Bahns

Collin said:
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/
 
K

Ken Bloom

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

Bertram Scharpf

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
 
7

7stud --

Collin said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top