# Deleting a line in a file

**URL:** <https://www.ruby-forum.com/t/deleting-a-line-in-a-file/141429>\
**Category:** Ruby\
**Created:** [June 25, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/deleting-a-line-in-a-file/141429 "2008-06-25T19:51:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 25, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/deleting-a-line-in-a-file/141429/1 "2008-06-25T19:51:32Z")

</div>

Hi, how do I delete a line in a file and remove the empty space  
afterward?

Thanks!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 25, 2008, 8:02pm UTC](https://www.ruby-forum.com/t/deleting-a-line-in-a-file/141429/2 "2008-06-25T20:02:49Z")

</div>

There are many ways to solve this.  
One I use often (which is not very elegant but rather easy) is to use  
File.readlines on your file, find the line in that array to kill it, and  
save the array to a file finally again.

BTW what do you mean with “empty space”? I guess it will be easy if you  
make a small file as example 🙂

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 25, 2008, 8:04pm UTC](https://www.ruby-forum.com/t/deleting-a-line-in-a-file/141429/3 "2008-06-25T20:04:26Z")

</div>

> BTW what do you mean with “empty space”? I guess it will be easy if you  
> make a small file as example 🙂

File:  
a  
b  
c  
d

program: delete(b)

File:  
a  
\<-- empty space  
c  
d

should be…

File:  
a  
c  
d

Thanks!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 25, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/deleting-a-line-in-a-file/141429/4 "2008-06-25T21:15:26Z")

</div>

On Thu, 2008-06-26 at 02:49 +0900, Justin To wrote:

> Hi, how do I delete a line in a file and remove the empty space  
> afterward?
> 
> Thanks!

grep -v “character string unique to line to be removed” file \> tmpfile  
mv tmpfile file

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 25, 2008, 8:19pm UTC](https://www.ruby-forum.com/t/deleting-a-line-in-a-file/141429/5 "2008-06-25T20:19:43Z")

</div>

This should get you started. There are probably 1000 ways but well,  
someone else can do that 🙂

The test\_file had this content:  
a  
b  
c  
d

The new\_file has  
a  
b  
d

class DeleteLine  
def initialize(read\_this\_file = ‘test\_file’)  
@file = File.readlines(read\_this\_file)  
delete\_line 3  
save\_to  
end  
def delete\_line(which\_line = 1)  
@file.delete\_at(which\_line-1) # we may count first line as 1, second  
line as 2 etc… otherwise remove the -1 part  
end  
def save\_to(this\_file = ‘new\_file’)  
File.open(this\_file, ‘w+’) {|f| f.write @file.join() }  
end  
end

DeleteLine.new

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 26, 2008, 5:36pm UTC](https://www.ruby-forum.com/t/deleting-a-line-in-a-file/141429/6 "2008-06-26T17:36:55Z")

</div>

> Justin To wrote:  
> Hi, how do I delete a line in a file and remove the empty space  
> afterward?
> 
> Thanks!

Some additional in-place file editing options on Unix:

/bin/ed -s file.txt \<\<\< $‘/b/d\nw’ # delete first line containing “b”

/bin/ed -s file.txt \<\<\< $‘g/b/d\nw’ # delete all lines containing “b”  
/usr/bin/sed -i “” -e ‘/b/d’ file.txt

/bin/ed -s file.txt \<\<\< $‘2,2d\nw’ # delete second line  
/usr/bin/sed -i “” -e ‘2,2d’ file.txt

More on the ed text editor here:

> **[bash-hackers.org - This website is for sale! - bash...](http://bash-hackers.org/wiki/doku.php?id=howto:edit-ed)**
>
> This website is for sale! bash-hackers.org is your first and best source for all of the information you’re looking for. From general topics to more of what you would expect to find here, bash-hackers.org has it all. We hope you find what you are...

Using Ruby from the command line:

ruby -p -i -e ‘m ||= 0; m == 1 ? $\_ : $_.sub!(/^.2.$/m, “”) || $_; m =  
1 unless $~.nil?’ file.txt  
ruby -p -i -e ‘$_.sub!(/^.b.$/m, “”)’ file.txt  
ruby -p -i -e '$_.sub!(/^.\*$/m, “”) if $. == 2’ file.txt

For more information on selective deletion of certain lines see:

> **[fepus.net](http://www.fepus.net/ruby1line.txt)**
>
> This domain may be for sale!

Cheers,

j.k.
