# Pulling out a specfic feild from a line

**URL:** <https://www.ruby-forum.com/t/pulling-out-a-specfic-feild-from-a-line/123039>\
**Category:** Ruby\
**Created:** [December 13, 2007, 4:32pm UTC](https://www.ruby-forum.com/t/pulling-out-a-specfic-feild-from-a-line/123039 "2007-12-13T16:32:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [December 13, 2007, 4:32pm UTC](https://www.ruby-forum.com/t/pulling-out-a-specfic-feild-from-a-line/123039/1 "2007-12-13T16:32:46Z")

</div>

Hey guys

Ive been looking for awhile now today trying to find a decent example of  
this and im really fustrated with it now.  
I need to get a specfic section of a line.  
lets sat this is the line

example 1 example 2 example 3

I want to get the third section "example 3"so i need to use tab as the  
sperater any suggestions?

Even a pointer to a section in the API that might help me would be  
usefull

regards  
Loftz

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [December 13, 2007, 4:34pm UTC](https://www.ruby-forum.com/t/pulling-out-a-specfic-feild-from-a-line/123039/2 "2007-12-13T16:34:29Z")

</div>

Sorry about the spelling btw

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [December 13, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/pulling-out-a-specfic-feild-from-a-line/123039/3 "2007-12-13T16:49:08Z")

</div>

Peter L. wrote:

> lets sat this is the line
> 
> example 1 example 2 example 3
> 
> I want to get the third section "example 3"so i need to use tab as the  
> sperater any suggestions?

“example 1\texample 2\texample 3”.split("\t")[2]

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [December 13, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/pulling-out-a-specfic-feild-from-a-line/123039/4 "2007-12-13T17:28:49Z")

</div>

ok but lets say im looping through an array full of different lengths  
and layouts of strings for instance

example example example  
exmaple/dfsd/asdf/ asdfasdf  
example 1 example2 example 3  
laskdjflkasdjfkl

im looking to see if the that third string on line 3 is actually there  
so passing very line into array and then checking each line

arr = Array.new  
i = 0  
File.foreach(example.txt) do |line|  
arr[i] = line  
i += 1  
end  
arr.each do |item|  
if (arr.to\_s.split("\t")[2] = “example 3”)  
puts “found it”  
end

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [December 13, 2007, 5:44pm UTC](https://www.ruby-forum.com/t/pulling-out-a-specfic-feild-from-a-line/123039/5 "2007-12-13T17:44:33Z")

</div>

Peter L. wrote:

> arr = Array.new  
> i = 0  
> File.foreach(example.txt) do |line|  
> Â Â arr[i] = line  
> Â Â i += 1  
> end

arr = File.readlines(“example.txt”)  
Does the same as the code above, but is far more compact.

> arr.each do |item|  
> if (arr.to\_s.split("\t")[2] = “example 3”)  
> Â puts “found it”  
> end  
> arr.to\_s will give you the whole content of the file. You want to use  
> item.

arr.map! {|line| line.split("\t")[2]}  
arr.each\_with\_index do |field, i|  
if field  
puts “Found field #{field} in line #{i}”  
else  
puts “No third field in line #{i}”  
end  
end

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [December 14, 2007, 9:46am UTC](https://www.ruby-forum.com/t/pulling-out-a-specfic-feild-from-a-line/123039/6 "2007-12-14T09:46:31Z")

</div>

2007/12/13, Peter L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> arr = Array.new  
> i = 0  
> File.foreach(example.txt) do |line|  
> arr[i] = line  
> i += 1  
> end  
> arr.each do |item|  
> if (arr.to\_s.split(“\t”)[2] = “example 3”)

You have an assignment here which is not what you want here.

> puts “found it”  
> end

This is a very inefficient way of doing things. You don’t need to  
store the whole file in an array if you are just interested in the  
presence test. Btw, you can even do this:

ruby -anF\t -e ‘if $F[2] == “example 3”; puts “found”; exit 0; end;  
END{ raise “not found”}’ your\_file

But then you’d probably use (g)awk anyway. 🙂

So, going back to Ruby:

File.foreach “file.txt” do |line|  
if line.chomp.split(/\t/)[2] == “example 3”  
puts “found”  
break # or: return or exit  
end  
end

Probably a bit more efficient:

File.foreach “file.txt” do |line|  
if /^(?:[^\t]\*\t){2}example 3\t/ =~ line  
puts “found”  
break # or: return or exit  
end  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [December 14, 2007, 10:16am UTC](https://www.ruby-forum.com/t/pulling-out-a-specfic-feild-from-a-line/123039/7 "2007-12-14T10:16:40Z")

</div>

> arr.map! {|line| line.split("\t")[2]}  
> arr.each\_with\_index do |field, i|  
> if field  
> puts “Found field #{field} in line #{i}”  
> else  
> puts “No third field in line #{i}”  
> end  
> end

thanks alot!

if i want to check that fields value how do i check it like it prints it  
out to the screen but i want to use something like this

if field = “example 3”  
puts “Found field #{field} in line #{i}”  
end  
but its still just spitting out each line

I dont want to print each line that has a third field just the specic  
one

---

<div class="post-metadata">

**Author:** ![jazcyk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@jazcyk](https://www.ruby-forum.com/u/jazcyk)\
**Post date:** [December 14, 2007, 11:06am UTC](https://www.ruby-forum.com/t/pulling-out-a-specfic-feild-from-a-line/123039/8 "2007-12-14T11:06:26Z")

</div>

I got it

```
flag = "FALSE"
arr = File.readlines(var1)
arr.map! {|line| line.split("\t")[2]}
arr.each_with_index do |line, i|
  if /#{var2}/ =~ line
  flag = "TRUE"
 end
  end
  if (flag == "TRUE")
puts "SUCCESS: This file " +var1 + " does contain the line"
  end
  if (flag == "FALSE")
puts "ERROR: This file " +var1 + " does not contain the line"
  end

```

thanks for the help
