Delete line from file

Hi,

Given a line number, what is the best way to delete this line from a
file?

And given an array of line numbers, what is the best way to delete
these lines from a file?

Thanks,

Jules

Jules wrote:

Hi,

Given a line number, what is the best way to delete this line from a
file?

And given an array of line numbers, what is the best way to delete
these lines from a file?

Thanks,

Jules

lines = IO.readlines('junk1')
[1,3,5].each{|n| lines.slice!(n) }
open('junk1','w'){|f| f.puts lines}

Given a line number, what is the best way to delete this line from a
file?

$ cat x
1
2
3
4
5
6
7
8
9
10

$ sed -ni -e '5 b;p' x

robert@fussel /cygdrive
$ cat x
1
2
3
4
6
7
8
9
10

Or did you mean with Ruby?

$ ruby -n -i.bak -e 'puts $_ unless $. == 5' x

$ cat x
1
2
3
4
6
7
8
9
10

And given an array of line numbers, what is the best way to delete
these lines from a file?

$ ruby -n -i.bak -e 'puts $_ unless [1,3,4].include? $.' x

robert@fussel /cygdrive/c/Temp
$ cat x
2
5
6
7
8
9
10

$ diff -u x.bak x
--- x.bak 2007-01-03 11:03:20.281250000 +0100
+++ x 2007-01-03 11:03:46.437500000 +0100
@@ -1,7 +1,4 @@
-1
  2
-3
-4
  5
  6
  7

Inside a script?

robert@fussel /cygdrive/c/Temp
$ ./del.rb x

robert@fussel /cygdrive/c/Temp
$ diff -u x.bak x
--- x.bak 2007-01-03 11:09:29.437500000 +0100
+++ x 2007-01-03 11:09:31.468750000 +0100
@@ -2,7 +2,6 @@
  2
  3
  4
-5
  6
  7
  8

robert@fussel /cygdrive/c/Temp
$ cat del.rb
#!ruby

f = ARGV.shift or raise "Need file name"
fb = f + ".bak"

File.rename(f, fb)

File.open(f, "w") do |out|
   File.foreach(fb) {|line| out.puts line unless $. == 5}
end

Plenty to choose from... :slight_smile:

Kind regards

  robert

···

On 03.01.2007 00:11, Jules wrote:

Thanks for these solutions!

Jules

Hi --

Jules wrote:

Hi,

Given a line number, what is the best way to delete this line from a
file?

And given an array of line numbers, what is the best way to delete
these lines from a file?

Thanks,

Jules

lines = IO.readlines('junk1')
[1,3,5].each{|n| lines.slice!(n) }

That's going to have a side-effect problem:

irb(main):004:0> a = %w{ a b c d e f }
=> ["a", "b", "c", "d", "e", "f"]
irb(main):005:0> [1,3,5].each {|e| a.slice!(e) }
=> [1, 3, 5]
irb(main):006:0> a
=> ["a", "c", "d", "f"]

You'd probably want to reverse the list of indices.

David

···

On Wed, 3 Jan 2007, William James wrote:

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Hi --

···

On Wed, 3 Jan 2007, Robert Klemme wrote:

On 03.01.2007 00:11, Jules wrote:

Given a line number, what is the best way to delete this line from a
file?

$ cat x
1
2
3
4
5
6
7
8
9
10

$ sed -ni -e '5 b;p' x

robert@fussel /cygdrive
$ cat x
1
2
3
4
6
7
8
9
10

Or did you mean with Ruby?

$ ruby -n -i.bak -e 'puts $_ unless $. == 5' x

$ cat x
1
2
3
4
6
7
8
9
10

And given an array of line numbers, what is the best way to delete
these lines from a file?

$ ruby -n -i.bak -e 'puts $_ unless [1,3,4].include? $.' x

robert@fussel /cygdrive/c/Temp
$ cat x
2
5
6
7
8
9
10

$ diff -u x.bak x
--- x.bak 2007-01-03 11:03:20.281250000 +0100
+++ x 2007-01-03 11:03:46.437500000 +0100
@@ -1,7 +1,4 @@
-1
2
-3
-4
5
6
7

Inside a script?

robert@fussel /cygdrive/c/Temp
$ ./del.rb x

robert@fussel /cygdrive/c/Temp
$ diff -u x.bak x
--- x.bak 2007-01-03 11:09:29.437500000 +0100
+++ x 2007-01-03 11:09:31.468750000 +0100
@@ -2,7 +2,6 @@
2
3
4
-5
6
7
8

robert@fussel /cygdrive/c/Temp
$ cat del.rb
#!ruby

f = ARGV.shift or raise "Need file name"
fb = f + ".bak"

File.rename(f, fb)

File.open(f, "w") do |out|
File.foreach(fb) {|line| out.puts line unless $. == 5}
end

Plenty to choose from... :slight_smile:

For the script version you can also do:

#!/usr/local/bin/ruby -ni.bak
puts $_ unless $. == 5

(or some variant thereof).

David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

dblack@wobblini.net wrote:

For the script version you can also do:

#!/usr/local/bin/ruby -ni.bak
puts $_ unless $. == 5

(or some variant thereof).

David

That is so "perl"......

dblack@wobblini.net wrote:

Hi --

>
> Jules wrote:
>> Hi,
>>
>> Given a line number, what is the best way to delete this line from a
>> file?
>>
>> And given an array of line numbers, what is the best way to delete
>> these lines from a file?
>>
>> Thanks,
>>
>> Jules
>
> lines = IO.readlines('junk1')
> [1,3,5].each{|n| lines.slice!(n) }

That's going to have a side-effect problem:

irb(main):004:0> a = %w{ a b c d e f }
=> ["a", "b", "c", "d", "e", "f"]
irb(main):005:0> [1,3,5].each {|e| a.slice!(e) }
=> [1, 3, 5]
irb(main):006:0> a
=> ["a", "c", "d", "f"]

True.

a = %w(zero one two three four five)
[1,3,5].each{|i| a[i]=nil}
a.compact!

···

On Wed, 3 Jan 2007, William James wrote:

dblack@wobblini.net wrote:

Hi --

>> Given a line number, what is the best way to delete this line from a
>> file?
>
> $ cat x
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
>
> $ sed -ni -e '5 b;p' x
>
> robert@fussel /cygdrive
> $ cat x
> 1
> 2
> 3
> 4
> 6
> 7
> 8
> 9
> 10
>
> Or did you mean with Ruby?
>
> $ ruby -n -i.bak -e 'puts $_ unless $. == 5' x
>
> $ cat x
> 1
> 2
> 3
> 4
> 6
> 7
> 8
> 9
> 10
>
>> And given an array of line numbers, what is the best way to delete
>> these lines from a file?
>
> $ ruby -n -i.bak -e 'puts $_ unless [1,3,4].include? $.' x
>
> robert@fussel /cygdrive/c/Temp
> $ cat x
> 2
> 5
> 6
> 7
> 8
> 9
> 10
>
> $ diff -u x.bak x
> --- x.bak 2007-01-03 11:03:20.281250000 +0100
> +++ x 2007-01-03 11:03:46.437500000 +0100
> @@ -1,7 +1,4 @@
> -1
> 2
> -3
> -4
> 5
> 6
> 7
>
> Inside a script?
>
> robert@fussel /cygdrive/c/Temp
> $ ./del.rb x
>
> robert@fussel /cygdrive/c/Temp
> $ diff -u x.bak x
> --- x.bak 2007-01-03 11:09:29.437500000 +0100
> +++ x 2007-01-03 11:09:31.468750000 +0100
> @@ -2,7 +2,6 @@
> 2
> 3
> 4
> -5
> 6
> 7
> 8
>
> robert@fussel /cygdrive/c/Temp
> $ cat del.rb
> #!ruby
>
> f = ARGV.shift or raise "Need file name"
> fb = f + ".bak"
>
> File.rename(f, fb)
>
> File.open(f, "w") do |out|
> File.foreach(fb) {|line| out.puts line unless $. == 5}
> end
>
>
> Plenty to choose from... :slight_smile:

For the script version you can also do:

#!/usr/local/bin/ruby -ni.bak
puts $_ unless $. == 5

The $_ can go.

print unless $. == 5

···

On Wed, 3 Jan 2007, Robert Klemme wrote:
> On 03.01.2007 00:11, Jules wrote:

Hi --

···

On Thu, 4 Jan 2007, akbarhome wrote:

dblack@wobblini.net wrote:

For the script version you can also do:

#!/usr/local/bin/ruby -ni.bak
puts $_ unless $. == 5

(or some variant thereof).

David

That is so "perl"......

I assume you're referring to the "(or some variant thereof)" advice
:slight_smile:

David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Hi --

···

On Thu, 4 Jan 2007, William James wrote:

dblack@wobblini.net wrote:

Hi --

On Wed, 3 Jan 2007, William James wrote:

Jules wrote:

Hi,

Given a line number, what is the best way to delete this line from a
file?

And given an array of line numbers, what is the best way to delete
these lines from a file?

Thanks,

Jules

lines = IO.readlines('junk1')
[1,3,5].each{|n| lines.slice!(n) }

That's going to have a side-effect problem:

irb(main):004:0> a = %w{ a b c d e f }
=> ["a", "b", "c", "d", "e", "f"]
irb(main):005:0> [1,3,5].each {|e| a.slice!(e) }
=> [1, 3, 5]
irb(main):006:0> a
=> ["a", "c", "d", "f"]

True.

a = %w(zero one two three four five)
[1,3,5].each{|i| a[i]=nil}
a.compact!

That's OK if you don't have any nils in the array you want to keep.
Reversing the index list should be pretty glitch-proof, I think.

David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

dblack@wobblini.net wrote:

Hi --

>
>> Hi --
>>
>>
>>>
>>> Jules wrote:
>>>> Hi,
>>>>
>>>> Given a line number, what is the best way to delete this line from a
>>>> file?
>>>>
>>>> And given an array of line numbers, what is the best way to delete
>>>> these lines from a file?
>>>>
>>>> Thanks,
>>>>
>>>> Jules
>>>
>>> lines = IO.readlines('junk1')
>>> [1,3,5].each{|n| lines.slice!(n) }
>>
>> That's going to have a side-effect problem:
>>
>> irb(main):004:0> a = %w{ a b c d e f }
>> => ["a", "b", "c", "d", "e", "f"]
>> irb(main):005:0> [1,3,5].each {|e| a.slice!(e) }
>> => [1, 3, 5]
>> irb(main):006:0> a
>> => ["a", "c", "d", "f"]
>
> True.
>
> a = %w(zero one two three four five)
> [1,3,5].each{|i| a[i]=nil}
> a.compact!

That's OK if you don't have any nils in the array you want to keep.
Reversing the index list should be pretty glitch-proof, I think.

ARGV.unshift 'junk2'
$-i = ".bak"
while gets do print unless [1,3].include?($.) end

Since these are lines from a file, there won't be any nils.

Here's a pretty short way to delete lines with backup:

ARGV.unshift 'junk2'
$-i = ".bak"
while gets do print unless [1,3].include?($.) end

···

On Thu, 4 Jan 2007, William James wrote:
> dblack@wobblini.net wrote:
>> On Wed, 3 Jan 2007, William James wrote:

Hi --

···

On Thu, 4 Jan 2007, William James wrote:

dblack@wobblini.net wrote:

Hi --

On Thu, 4 Jan 2007, William James wrote:

dblack@wobblini.net wrote:

Hi --

On Wed, 3 Jan 2007, William James wrote:

Jules wrote:

Hi,

Given a line number, what is the best way to delete this line from a
file?

And given an array of line numbers, what is the best way to delete
these lines from a file?

Thanks,

Jules

lines = IO.readlines('junk1')
[1,3,5].each{|n| lines.slice!(n) }

That's going to have a side-effect problem:

irb(main):004:0> a = %w{ a b c d e f }
=> ["a", "b", "c", "d", "e", "f"]
irb(main):005:0> [1,3,5].each {|e| a.slice!(e) }
=> [1, 3, 5]
irb(main):006:0> a
=> ["a", "c", "d", "f"]

True.

a = %w(zero one two three four five)
[1,3,5].each{|i| a[i]=nil}
a.compact!

That's OK if you don't have any nils in the array you want to keep.
Reversing the index list should be pretty glitch-proof, I think.

ARGV.unshift 'junk2'
$-i = ".bak"
while gets do print unless [1,3].include?($.) end

Since these are lines from a file, there won't be any nils.

True. I'd meandered away from the original question a bit.

David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)