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:

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

Hi –

On Wed, 3 Jan 2007, William J. wrote:

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 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:

Kind regards

robert

Thanks for these solutions!

Jules

Hi –

On Wed, 3 Jan 2007, Robert K. wrote:

6
2
$ ruby -n -i.bak -e ‘puts $_ unless $. == 5’ x
10
6
2

7

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

[email protected] wrote:

David

That is so “perl”…

Hi –

On Thu, 4 Jan 2007, akbarhome wrote:

David

That is so “perl”…

I assume you’re referring to the “(or some variant thereof)” advice
:slight_smile:

David

[email protected] wrote:

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!

Hi –

On Thu, 4 Jan 2007, William J. wrote:

Given a line number, what is the best way to delete this line from a
[1,3,5].each{|n| lines.slice!(n) }
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

Hi –

On Thu, 4 Jan 2007, William J. wrote:

Thanks,
irb(main):005:0> [1,3,5].each {|e| a.slice!(e) }
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

[email protected] wrote:

2

10
6
robert@fussel /cygdrive/c/Temp
— x.bak 2007-01-03 11:03:20.281250000 +0100
Inside a script?
3
f = ARGV.shift or raise “Need file name”

For the script version you can also do:

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

The $_ can go.

print unless $. == 5

[email protected] wrote:

=> [1, 3, 5]
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