Reading particular text from a txt file

I wrote the following code to read a particular line from a file :

f = File.open(‘F:\Testing123.txt’)
a = f.readlines
puts a[7]

The Output is :
CRP+TYH+111111’

What I want :

I only want 111111 returned from the file.
Can anyone pls tell me the code for that?

Thanks

Lekha Selvaraj wrote in post #1050186:

I wrote the following code to read a particular line from a file :

f = File.open(‘F:\Testing123.txt’)
a = f.readlines

You read more than necessary. You just need to read 8 lines.

puts a[7]

Btw, you are not closing the file properly.
http://blog.rubybestpractices.com/posts/rklemme/001-Using_blocks_for_Robustness.html

The Output is :
CRP+TYH+111111’

What I want :

I only want 111111 returned from the file.
Can anyone pls tell me the code for that?

def r(f)
File.open f do |io|
io.each_with_index do |line, num|
return line[/^CRP+TYH+(\d+)/, 1] if num == 7
end
end

nil
end

or

def r(f)
File.open f do |io|
line = io.first(8)[-1] and line[/^CRP+TYH+(\d+)/, 1]
end
end

Kind regards

robert

Thanks Robert.

Like I said. Am new to ruby. I dint know what am doing.

From what you said.

I wrote something like this

f = File.open(‘F:\testing.txt’)
def r(f)
File.open f do |io|
line = io.first(8)[-1] and line[/^CRP+TYH+(\d+)/, 1]
puts line
end
end

I know this is not right coz I didn’t get the output. How should I
re-write it? to just get the output 111111

Lekha Selvaraj wrote in post #1050192:

I know this is not right coz I didn’t get the output. How should I
re-write it? to just get the output 111111

Please post a sample of the file so we can test ourselves.

Cheers

robert

Here you go

Lekha Selvaraj wrote in post #1050195:

Here you go

The line you look for is in line 10 and not 8. From your initial
posting it seemed the line was fixed.

def r(f)
File.foreach f do |line|
match = line[/^CRP+TYH+(\d+)/, 1] and return match
end

nil
end

Cheers

robert

This is the code you want:

str = File.open(“status.txt”) { |f| f.read }
m = /^CRP+TYH+(\d+)/.match(str)
puts m[1]

Its confusing because .match() returns an array of matches where m[0] is
the whole line, and m[1] is the part you’re looking for (between
parentasis).

Best,
Eric

visualruby.net

Sorry about the confusion. The line remains on the 10th place only.

I tried this :

def r(f)
f = File.open(‘F:\Testing123.txt’)
File.foreach f do |line|
match = line[/^CRP+TYH+(\d+)/, 1] and return match
puts match
end
nil
end

But its not returning anything in ‘match’ and ‘puts’ is not printing
anything for me to see.

Eric you are awesome. It worked like a gem :slight_smile:

Eric C. wrote in post #1050315:

This is the code you want:

str = File.open(“status.txt”) { |f| f.read }
m = /^CRP+TYH+(\d+)/.match(str)
puts m[1]

That code has several issues:

  1. It will break (throw an exception) if the string is not found in the
    file because it does
    not check whether m is non nil.
  2. File.read() is a much simpler way to read the whole file at once than
    File.open(…){|f| f.read}.
  3. It is inefficient because it reads in the whole file just to extract
    a portion of the file.
  4. Even worse, it will break for large files.
  5. It does more IO than necessary because it always reads the whole
    file.
  6. Logic is not encapsulated in a method which hinders reuse.

Compare that to my solution:

def r(f)
File.foreach f do |line|
match = line[/^CRP+TYH+(\d+)/, 1] and return match
end

nil
end

The file is processed line by line (but still IO uses buffering
underneath for efficiency). Reading is stopped as soon as the data is
found. It won’t break for large files.

We can even generalize the approach

def find_first_in_file(file, regexp, group = 1)
File.foreach file do |line|
match = regexp.match(line) and return match[group] || match[0]
end

nil
end

This will return the first match and if the regexp has a capturing group
it will return the capture of the first group. We would use it like
this

data = find_first_file(“myfile.txt”, /^CRP+TYH+(\d+)/)

Kind regards

robert

If I understand you correctly, you want to read the second and third
entry of the first line beginning with ‘STS’. You can do this by using
capture groups in the regular expression (like Robert and Eric already
did in the posts above).

For example:

File.foreach ‘F:/testing.txt’ |line|
if line =~ /\ASTS+(\w+)+(\w+)/
puts $1, $2
break
end
end

When the regular expression is applied, the matched content of the
parenthesized subexpressions are saved in the variables $1 und $2. You
can then output these variables.

However, you should write a general method like Robert suggested. Then
you won’t have to think about new code every time you want to read a
certain line but simply use the method.

Thanks for the input Robert. I will look into them.

Am having trouble with understanding the syntax in ruby.

My txt file has this line which I need to read.

STS+AAR+MQT’

My code :
f = File.open(‘F:\testing.txt’) { |f| f.read }
a = /^STS/.match(str)
puts a[0]

The output I got :
STS

Rather I want AAR in one variable and MQT in another variable.

So that my output will be
AAR
MQT

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