[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Delete line from file

Jules

1/2/2007 11:12:00 PM

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

12 Answers

William James

1/2/2007 11:20:00 PM

0


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}

dblack

1/2/2007 11:30:00 PM

0

Robert Klemme

1/3/2007 10:13:00 AM

0

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

Kind regards

robert

dblack

1/3/2007 10:50:00 AM

0

Jules

1/3/2007 12:36:00 PM

0

Thanks for these solutions!

Jules

akbarhome

1/3/2007 4:15:00 PM

0

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

1/3/2007 4:21:00 PM

0

William James

1/3/2007 7:32:00 PM

0


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!

dblack

1/3/2007 8:18:00 PM

0

William James

1/3/2007 9:21:00 PM

0

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.

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