Delete line from file

J

Jules

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
 
W

William James

Jules said:
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}
 
D

dblack

Hi --

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

--
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)
 
R

Robert Klemme

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

Kind regards

robert
 
D

dblack

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

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)
 
A

akbarhome

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"......
 
W

William James

Hi --

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=nil}
a.compact!
 
D

dblack

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=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)
 
W

William James

Hi --

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=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
 
W

William James

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

For the script version you can also do:

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

The $_ can go.

print unless $. == 5
 
D

dblack

Hi --

Hi --

(e-mail address removed) 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=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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,137
Members
47,754
Latest member
Armand37T7

Latest Threads

Top