# Take text from file and add do it with Ruby..?

**URL:** <https://www.ruby-forum.com/t/take-text-from-file-and-add-do-it-with-ruby/136772>\
**Category:** Ruby\
**Created:** [May 6, 2008, 10:34am UTC](https://www.ruby-forum.com/t/take-text-from-file-and-add-do-it-with-ruby/136772 "2008-05-06T10:34:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [May 6, 2008, 10:34am UTC](https://www.ruby-forum.com/t/take-text-from-file-and-add-do-it-with-ruby/136772/1 "2008-05-06T10:34:17Z")

</div>

I have a text file that I want to add some content to, but I’m not sure  
how to use the file as input. I have a list of things in the file that  
is formatted like

item1 | item2 | item3 | item4 |

and etcetera… How can I take these items and put each of them on a new  
line, minus the “|” and add a certain phrase afterward? Like:

item1 [phrase]  
item2 [phrase]  
item3 [phrase]

Is there a simple way to do this? I’ve taken lists of things like this  
before and added text to it, but I put all of the items in an array  
manually first… would someone show me a faster way, please?

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [May 6, 2008, 11:19am UTC](https://www.ruby-forum.com/t/take-text-from-file-and-add-do-it-with-ruby/136772/2 "2008-05-06T11:19:26Z")

</div>

Try this:

ph\_hashes = Hash.new(“no phrase”).merge({  
:item1 =\> “phrase 1”,  
:item2 =\> “phrase 2”,  
:item3 =\> “phrase 3”  
})

File.open(“yourfile\_o.txt”,“w”) do |file|  
file.write  
File.open(“yourfile.txt”).readlines.join("\n").gsub(/([^|]+)|/){  
“#{(v=$1.strip) + " " + ph\_hashes[v.to\_sym]}\n”}  
end

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [May 6, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/take-text-from-file-and-add-do-it-with-ruby/136772/3 "2008-05-06T12:02:59Z")

</div>

> Hi Zoe,  
> Ruby program
> 
> * * *
> 
> full= File.open(“orig.txt”)  
> phrase=[“phrase1”,“phrase2”,“phrase3”]  
> count=0  
> full.each do |line|  
> first=[]  
> first=line.split(/|/)  
> first.each do |single|  
> sub=single.strip!  
> main = (sub).to\_s + (phrase [count]).to\_s  
> puts main  
> count+=1  
> end  
> end

how can I save the output to a file? I see where you’re making it print  
the output with “main”, but when I try to save it to a file the way I  
have with other programs I’ve made, I get an error… it says the  
variable “main” is undefined. Help?

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [May 6, 2008, 11:20am UTC](https://www.ruby-forum.com/t/take-text-from-file-and-add-do-it-with-ruby/136772/4 "2008-05-06T11:20:17Z")

</div>

Zoe P. wrote:

> I have a text file that I want to add some content to, but I’m not sure  
> how to use the file as input. I have a list of things in the file that  
> is formatted like
> 
> item1 | item2 | item3 | item4 |
> 
> and etcetera… How can I take these items and put each of them on a new  
> line, minus the “|” and add a certain phrase afterward? Like:
> 
> item1 [phrase]  
> item2 [phrase]  
> item3 [phrase]
> 
> Is there a simple way to do this? I’ve taken lists of things like this  
> before and added text to it, but I put all of the items in an array  
> manually first… would someone show me a faster way, please?

Hi Zoe,  
Ruby program

* * *

full= File.open(“orig.txt”)  
phrase=[“phrase1”,“phrase2”,“phrase3”]  
count=0  
full.each do |line|  
first=[]  
first=line.split(/|/)  
first.each do |single|  
sub=single.strip!  
main = (sub).to\_s + (phrase [count]).to\_s  
puts main  
count+=1  
end  
end

* * *

Orig.txt

* * *

item1 | item2 | item3 | item4 |  
item11 | item21 | item31 | item41 |  
item12 | item23 | item34 | item45 |  
item13 | item23 | item33 | item43 |  
item14 | item24 | item34 | item44 |

* * *

Output

* * *

> ruby file.rb  
> item1phrase1  
> item2phrase2  
> item3phrase3  
> item4

item11  
item21  
item31  
item41

item12  
item23  
item34  
item45

item13  
item23  
item33  
item43

item14  
item24  
item34  
item44

> Exit code: 0

* * *

I dont know this one is very simple but it satisfies ur need

Regards,  
P.Raveendran  
RailsFactory

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [May 6, 2008, 12:50pm UTC](https://www.ruby-forum.com/t/take-text-from-file-and-add-do-it-with-ruby/136772/5 "2008-05-06T12:50:42Z")

</div>

Try this one…

full= File.open(“orig.txt”)  
phrase=[“phrase1”,“phrase2”,“phrase3”]  
count=0  
inside=[]  
full.each do |line|  
first=[]  
first=line.split(/|/)  
first.each do |single|  
sub=single.strip!  
main = (sub).to\_s + " "+(phrase [count]).to\_s

```
inside << main

newfile=File.new("orig2","w")
newfile.puts inside
newfile.close

count+=1

```

end  
end

Regards,  
P.Raveendran  
RailsFactory

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [May 6, 2008, 1:44pm UTC](https://www.ruby-forum.com/t/take-text-from-file-and-add-do-it-with-ruby/136772/6 "2008-05-06T13:44:04Z")

</div>

ahh, I see… thank you 🙂
