# Simple text parsing

**URL:** <https://www.ruby-forum.com/t/simple-text-parsing/186541>\
**Category:** Ruby\
**Created:** [April 8, 2010, 4:45pm UTC](https://www.ruby-forum.com/t/simple-text-parsing/186541 "2010-04-08T16:45:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Remington\_Splettsto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@Remington\_Splettsto](https://www.ruby-forum.com/u/Remington_Splettsto)\
**Post date:** [April 8, 2010, 4:45pm UTC](https://www.ruby-forum.com/t/simple-text-parsing/186541/1 "2010-04-08T16:45:24Z")

</div>

Hello Everyone,

What is the best way to get this input

## test.txt

## David | Hansson | Chicago Obie | Fernandez | Jacksonville

to this output:

First Name: David  
Last Name: Hansson  
City: Chicago

First Name: Obie  
Last Name: Fernandez  
City: Jacksonville

???

This is what I’ve tried so far… I’m pretty far off I believe. Any  
suggestions would help with learning the ins and outs of Ruby (or  
programming in general)

* * *

## test.rb

myArray = []  
file = File.open(“test.txt”, ‘r+’)  
file.each\_line do |line|  
x = line.scan(/\w+/).inspect  
myArray = x  
puts myArray  
end

## my output

$ ruby test.rb  
[“David”, “Hansson”, “Chicago”]  
[“Obie”, “Fernandez”, “Jacksonville”]

Thanks in Advance  
Remington Splettstoesser

---

<div class="post-metadata">

**Author:** ![Remington\_Splettsto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@Remington\_Splettsto](https://www.ruby-forum.com/u/Remington_Splettsto)\
**Post date:** [April 8, 2010, 5:55pm UTC](https://www.ruby-forum.com/t/simple-text-parsing/186541/2 "2010-04-08T17:55:39Z")

</div>

“Remington Splettstoesser” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…  
Hello Everyone,

What is the best way to get this input

## test.txt

## David | Hansson | Chicago Obie | Fernandez | Jacksonville

to this output:

First Name: David  
Last Name: Hansson  
City: Chicago

First Name: Obie  
Last Name: Fernandez  
City: Jacksonville

???

This is what I’ve tried so far… I’m pretty far off I believe. Any  
suggestions would help with learning the ins and outs of Ruby (or  
programming in general)

* * *

## test.rb

myArray = []  
file = File.open(“test.txt”, ‘r+’)  
file.each\_line do |line|  
x = line.scan(/\w+/).inspect  
myArray = x  
puts myArray  
end

## my output

$ ruby test.rb  
[“David”, “Hansson”, “Chicago”]  
[“Obie”, “Fernandez”, “Jacksonville”]

Thanks in Advance  
Remington Splettstoesser

A solution is to use printf as in the folloving code:

# test1.rb

file = File.open(“test.txt”, ‘r+’)  
file.each\_line do |line|  
myArray = line.scan(/\w+/)

# myArray = line.split(/[\s|]+/) Alternative solution to scan

printf(“\nFirst Name: %-s\nLast Name : %-s\nCity : %-s\n”,  
\*myArray)  
end

Hth gfb

---

<div class="post-metadata">

**Author:** ![Remington\_Splettsto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@Remington\_Splettsto](https://www.ruby-forum.com/u/Remington_Splettsto)\
**Post date:** [April 8, 2010, 6:02pm UTC](https://www.ruby-forum.com/t/simple-text-parsing/186541/3 "2010-04-08T18:02:06Z")

</div>

Watch out, both of these solutions will fail for

John | O’Brian | New York

---

<div class="post-metadata">

**Author:** ![Remington\_Splettsto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@Remington\_Splettsto](https://www.ruby-forum.com/u/Remington_Splettsto)\
**Post date:** [April 8, 2010, 10:37pm UTC](https://www.ruby-forum.com/t/simple-text-parsing/186541/4 "2010-04-08T22:37:16Z")

</div>

Thank You gfb!

Remington

---

<div class="post-metadata">

**Author:** ![Remington\_Splettsto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/43a26b/32.png) [@Remington\_Splettsto](https://www.ruby-forum.com/u/Remington_Splettsto)\
**Post date:** [April 8, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/simple-text-parsing/186541/5 "2010-04-08T18:30:48Z")

</div>

At 2010-04-08 10:44AM, “Remington Splettstoesser” wrote:

> to this output:
> 
> First Name: David  
> Last Name: Hansson  
> City: Chicago
> 
> First Name: Obie  
> Last Name: Fernandez  
> City: Jacksonville

Looks like you have a specific field separator, so might as well use it:

```
print_format = "First Name: %s\nLast Name: %s\nCity: %s\n\n"

File.foreach("test.txt") do |line|
  first, last, city = line.chomp.split(/ \| /)
  print print_format % [first, last, city]
end
```
