# String manipulation: cut, insert, column editing

**URL:** <https://www.ruby-forum.com/t/string-manipulation-cut-insert-column-editing/145677>\
**Category:** Ruby\
**Created:** [August 12, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/string-manipulation-cut-insert-column-editing/145677 "2008-08-12T21:11:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [August 12, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/string-manipulation-cut-insert-column-editing/145677/1 "2008-08-12T21:11:34Z")

</div>

Hi all,

Is it possible to use ruby like awk, sed or cut in unix to editing  
strings?

Let say I have to write INSERT query like this:

This is the first (1Â°) item.  
This is the second (2Â°) item.  
This is the third (3Â°) item.  
…

In one file I have the word-number (first, second, third) and in another  
file I have the real number (1Â°, 2Â°, 3Â°) of the item, both on each line.

Of course, I don’t want to really write it, so how can I “edit” the  
number of the item dinamically? I’ve tried with regex, but I haven’t  
found a solution…

I think that with some text editors maybe it’s possible to do that, but  
I would like to know if with ruby the result can be achieved easier.

Let me know.

Thanks.

Best regards.

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [August 13, 2008, 3:46am UTC](https://www.ruby-forum.com/t/string-manipulation-cut-insert-column-editing/145677/2 "2008-08-13T03:46:17Z")

</div>

Toki T. wrote:

> Let say I have to write INSERT query like this:
> 
> This is the first (1Â°) item.  
> This is the second (2Â°) item.  
> This is the third (3Â°) item.  
> …

I don’t think this would be the best solution, but you could map 1 =\>  
“first”… and then use regex to identify the number and get its mapped  
value. Problem is that it runs one way and limited.

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [August 13, 2008, 5:55am UTC](https://www.ruby-forum.com/t/string-manipulation-cut-insert-column-editing/145677/3 "2008-08-13T05:55:43Z")

</div>

On Aug 12, 3:09 pm, Toki T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …
> 
> In one file I have the word-number (first, second, third) and in another  
> file I have the real number (1°, 2°, 3°) of the item, both on each line.

Here’s some Ruby code that may do what you’re interested in:

====

d1 = open(“f1.txt”) do |f|  
f.readlines.map { |line| line.chomp }  
end

d2 = open(“f2.txt”) do |f|  
f.readlines.map { |line| line.chomp }  
end

d3 = [d1, d2].transpose

p d1, d2, d3 # just so you can see what’s happened so  
far

d3.each do |str1, str2|  
puts “This is the #{str1} (#{str2}) item.”  
end

====

That’s assuming f1.txt contains “first”, “second”, etc., one per line  
and that f2.txt contains “1st”, “2nd”, etc., one per line.

Eric

====

Are you looking for on-site Ruby or Ruby on Rails training  
that’s been highly reviewed by former students?  
[http://LearnRuby.com](http://LearnRuby.com)

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [August 17, 2008, 1:12pm UTC](https://www.ruby-forum.com/t/string-manipulation-cut-insert-column-editing/145677/4 "2008-08-17T13:12:55Z")

</div>

Eric I. wrote:

> Here’s some Ruby code that may do what you’re interested in:
> 
> ====
> 
> d1 = open(“f1.txt”) do |f|  
> f.readlines.map { |line| line.chomp }  
> end
> 
> d2 = open(“f2.txt”) do |f|  
> f.readlines.map { |line| line.chomp }  
> end
> 
> d3 = [d1, d2].transpose
> 
> p d1, d2, d3 # just so you can see what’s happened so  
> far
> 
> d3.each do |str1, str2|  
> puts “This is the #{str1} (#{str2}) item.”  
> end
> 
> ====
> 
> That’s assuming f1.txt contains “first”, “second”, etc., one per line  
> and that f2.txt contains “1st”, “2nd”, etc., one per line.
> 
> Eric
> 
> ====

Thanks a lot for the code, it works great and it’s really clear.

Best regards.

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [August 13, 2008, 9:37am UTC](https://www.ruby-forum.com/t/string-manipulation-cut-insert-column-editing/145677/5 "2008-08-13T09:37:02Z")

</div>

2008/8/12 Toki T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Is it possible to use ruby like awk, sed or cut in unix to editing  
> strings?

These are quite different tools but yes, you can use Ruby like them  
most of the time. Please also have a look at command line parameters  
of the Ruby interpreter (ruby -h).

> Let say I have to write INSERT query like this:
> 
> This is the first (1°) item.  
> This is the second (2°) item.  
> This is the third (3°) item.  
> …

This does not look like an SQL INSERT statement, what is it?

> In one file I have the word-number (first, second, third) and in another  
> file I have the real number (1°, 2°, 3°) of the item, both on each line.

Not sure what exactly you are trying to do. For the loading part  
here’s another variant:

h = {}

File.open “f1.txt” do |io1|  
File.open “f2.txt” do |io2|  
io1.zip io2 do |k,v|  
h[k.chomp] = v.chomp  
end  
end  
end

p h

> Of course, I don’t want to really write it, so how can I “edit” the  
> number of the item dinamically? I’ve tried with regex, but I haven’t  
> found a solution…
> 
> I think that with some text editors maybe it’s possible to do that, but  
> I would like to know if with ruby the result can be achieved easier.

Well, if you tell us what exactly you are trying to achieve. At least  
to me it’s not fully clear.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![toki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@toki](https://www.ruby-forum.com/u/toki)\
**Post date:** [August 17, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/string-manipulation-cut-insert-column-editing/145677/6 "2008-08-17T14:04:58Z")

</div>

Hi Eric –

On Wed, 13 Aug 2008, Eric I. wrote:

> > This is the third (3°) item.  
> > f.readlines.map { |line| line.chomp }  
> > end

You can even skip the readlines and just map the IO handle:

f.map {|line| line.chomp }

David
