# Need to insert row in particular line in text file in ruby

**URL:** <https://www.ruby-forum.com/t/need-to-insert-row-in-particular-line-in-text-file-in-ruby/253328>\
**Category:** Uncategorized\
**Created:** [November 13, 2019, 1:38pm UTC](https://www.ruby-forum.com/t/need-to-insert-row-in-particular-line-in-text-file-in-ruby/253328 "2019-11-13T13:38:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rravi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@rravi](https://www.ruby-forum.com/u/rravi)\
**Post date:** [November 13, 2019, 1:38pm UTC](https://www.ruby-forum.com/t/need-to-insert-row-in-particular-line-in-text-file-in-ruby/253328/1 "2019-11-13T13:38:50Z")

</div>

hi All,

i have one text file with 100’s of lines and my scenarios is i need to insert a new line at row number 36 in text file… please help me how to do as am new to ruby script.

---

<div class="post-metadata">

**Author:** ![G4143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a88e57/32.png) [@G4143](https://www.ruby-forum.com/u/G4143)\
**Post date:** [November 13, 2019, 1:57pm UTC](https://www.ruby-forum.com/t/need-to-insert-row-in-particular-line-in-text-file-in-ruby/253328/2 "2019-11-13T13:57:44Z")

</div>

They are a few ways to do this but I would choose the simple way.

1. Open the file and read all the data into a array.
2. Insert the new line into the position of the array.
3. Save the array to file with the original name.

---

<div class="post-metadata">

**Author:** ![G4143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a88e57/32.png) [@G4143](https://www.ruby-forum.com/u/G4143)\
**Post date:** [November 13, 2019, 2:32pm UTC](https://www.ruby-forum.com/t/need-to-insert-row-in-particular-line-in-text-file-in-ruby/253328/3 "2019-11-13T14:32:36Z")

</div>

Here’s the basic Ruby stuff to accomplish your request:

```
# datafile contents
# -----------------
# This is the first
# This is the second
# This is the fourth

print "Enter filename: "
filename = gets.chomp

ifile = File.new(filename)

data = ifile.readlines # read file contents into array - data

ifile.close

p data # display data array

data.insert(2, "This is the third\n") # insert new line into data array

p data # display data array

File.write(filename, data.join, mode: "w") # write(overwrite) data to original file

# datafile contents at the end
# -----------------
# This is the first
# This is the second
# This is the third
# This is the fourth
```

---

<div class="post-metadata">

**Author:** ![rravi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@rravi](https://www.ruby-forum.com/u/rravi)\
**Post date:** [December 7, 2019, 5:31am UTC](https://www.ruby-forum.com/t/need-to-insert-row-in-particular-line-in-text-file-in-ruby/253328/4 "2019-12-07T05:31:03Z")

</div>

thanks, can i have sample code…

---

<div class="post-metadata">

**Author:** ![Lagmar](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/lagmar/32/1317_2.png) [@Lagmar](https://www.ruby-forum.com/u/Lagmar)\
**Post date:** [February 6, 2020, 5:33pm UTC](https://www.ruby-forum.com/t/need-to-insert-row-in-particular-line-in-text-file-in-ruby/253328/5 "2020-02-06T17:33:30Z")

</div>

But there is no line 36 …
