Delete a lines from a files

Hi
I want to delete a line from a file after finding the lines

example

f.open(“test.txt”,“w”)

f.each do |item|
if item=~^john
… delete the line …

end

how can i execute the delete commands for that line in that files??

Thanks

Thanks

Hi,

You cannot delete a line from a file. But you can overwrite the file and
skip this line.

If the file isn’t too big, you may also read it completely, delete the
line from the resulting string and then write the string to the file
again.

On 12/08/2012, at 1:28 AM, “Jan E.” [email protected] wrote:

Hi,

You cannot delete a line from a file. But you can overwrite the file and
skip this line.

Write your changed data to a file with different name, the rename (move)
the new file to the original file. This will reduce the probability of
file corruption.

Henry

Jan E. wrote in post #1072045:

You cannot delete a line from a file. But you can overwrite the file and
skip this line.

Hi thanks
but sorry for late reply.
but how do you skip the line ???
what would be the steps
thanks

Fosiul A. wrote in post #1072077:

Hi thanks
but sorry for late reply.
but how do you skip the line ???

In your loop, you will be reading each line, and writing it to the
output file. So just don’t write it if you don’t want it in the output.

Robert K. wrote in post #1072081:

If you really only need to delete a single line from a file sed is
probably the easiest choice:

without backup
$ sed -i ‘/^john/ d’ your_file

with backup
$ sed -i.bak ‘/^john/ d’ your_file

Kind regards

robert

Hi,

I think because this discussion is about Ruby and the OP may or may not
have access to sed, we can follow Dave T.’ way on the command line:

ruby -e ‘BEGIN{$/=nil}; puts STDIN.readlines.to_s.gsub(/^john.*$/, “”)’
< your_file

I haven’t tried the code myself yet, so it has to be taken with a grain
of salt; and as written, it only outputs to stdout.

Regards,

Bill

On Sun, Aug 12, 2012 at 3:00 PM, Fosiul A. [email protected]
wrote:

what would be the steps
thanks

If you really only need to delete a single line from a file sed is
probably the easiest choice:

without backup
$ sed -i ‘/^john/ d’ your_file

with backup
$ sed -i.bak ‘/^john/ d’ your_file

Kind regards

robert

On Sun, Aug 12, 2012 at 7:49 PM, Admin T. [email protected]
wrote:

Robert K. wrote in post #1072081:

If you really only need to delete a single line from a file sed is
probably the easiest choice:

without backup
$ sed -i ‘/^john/ d’ your_file

with backup
$ sed -i.bak ‘/^john/ d’ your_file

I think because this discussion is about Ruby and the OP may or may not
have access to sed, we can follow Dave T.’ way on the command line:

ruby -e ‘BEGIN{$/=nil}; puts STDIN.readlines.to_s.gsub(/^john.*$/, “”)’
< your_file

Why do you set the input record separator to nil? How then should
readlines work? Also, I hope you are aware that readlines will read
in the whole input before outputting anything. This is quite
inefficient for medium to large files. ARGF might be better, too.

I haven’t tried the code myself yet, so it has to be taken with a grain
of salt; and as written, it only outputs to stdout.

Well, there is a Ruby solution closer to sed solution I posted earlier:

without backup

$ ruby19 -i -ne ‘puts $_ unless /^john/’ your_file

with backup

$ ruby19 -i.bak -ne ‘puts $_ unless /^john/’ your_file

Kind regards

robert

Guys, you really need to stop wanking off over your great Ruby skills
and maybe post a solution that an average user can actually understand
and integrate in his code.

We all know you are the greatest Ruby hackers on earth with the fanciest
one-liners ever. But I think we’re here to help and not to masturbate
all day long.

What about a simple solution?

old_file = ‘test.txt’
new_file = ‘test2.txt’
File.open new_file, ‘w’ do |file|

loop over the lines of old_file and write those

not beginning with “john” to new_file

File.foreach old_file do |line|
file << line unless line.start_with? ‘john’
end
end

Jan E. wrote in post #1072105:

Guys, you really need to stop wanking off over your great Ruby skills
and maybe post a solution that an average user can actually understand
and integrate in his code.

LOl thanks , those one liner was bit hard for me to understand …

We all know you are the greatest Ruby hackers on earth with the fanciest
one-liners ever. But I think we’re here to help and not to masturbate
all day long.

What about a simple solution?

old_file = ‘test.txt’
new_file = ‘test2.txt’
File.open new_file, ‘w’ do |file|

loop over the lines of old_file and write those

not beginning with “john” to new_file

File.foreach old_file do |line|
file << line unless line.start_with? ‘john’
end
end

this code will work perfect …

thanks

Robert K. wrote in post #1072104:

ruby -e ‘BEGIN{$/=nil}; puts STDIN.readlines.to_s.gsub(/^john.*$/, “”)’
< your_file

Why do you set the input record separator to nil? How then should
readlines work? Also, I hope you are aware that readlines will read
in the whole input before outputting anything. This is quite
inefficient for medium to large files. ARGF might be better, too.

Well, I haven’t tried the code yet. For your questions, probably better
to ask Dave T… :slight_smile: I took it from his one liners.

Regards,

Bill

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs