how to delete the first two characters of each row?

P

Pen Ttt

how to delete the first two characters of each row?
i want to delete the first two characters of each row in file
/home/pt/test/mtp.txt


io=open("/home/pt/test/mtp.txt","r")
io1=open("/home/pt/test/mtpback.txt","w")
while line=io.gets
ioput=line.delete("/^..../")
io1.write(ioput)
end

when i open /home/pt/test/mtpback.txt,there is not what i want,where is
wrong?
 
A

Alex DeCaria

Pen said:
how to delete the first two characters of each row?
i want to delete the first two characters of each row in file
/home/pt/test/mtp.txt


io=open("/home/pt/test/mtp.txt","r")
io1=open("/home/pt/test/mtpback.txt","w")
while line=io.gets
ioput=line.delete("/^..../")
io1.write(ioput)
end

when i open /home/pt/test/mtpback.txt,there is not what i want,where is
wrong?

You could do

io=open("/home/pt/test/mtp.txt","r")
io1=open("/home/pt/test/mtpback.txt","w")
while line=io.gets
ioput=line[2..line.length] # extract substring without first two
characters
io1.write(ioput)
end
 
B

Brian Candler

Pen said:
how to delete the first two characters of each row?
i want to delete the first two characters of each row in file
/home/pt/test/mtp.txt


io=open("/home/pt/test/mtp.txt","r")
io1=open("/home/pt/test/mtpback.txt","w")
while line=io.gets
ioput=line.delete("/^..../")

You are trying to delete exactly the string of characters "/^..../"

You need to match against a regular expression instead. For example,

ioput=line.sub(/^..../, '')

(That will delete the first 4 characters in each line, but only if there
are 4 or more)
 
R

Robert Klemme

2010/4/13 Pen Ttt said:
how to delete the first two characters =A0of each row?
i want to delete the first two characters =A0of each row in file
/home/pt/test/mtp.txt


io=3Dopen("/home/pt/test/mtp.txt","r")
io1=3Dopen("/home/pt/test/mtpback.txt","w")
while line=3Dio.gets
=A0 ioput=3Dline.delete("/^..../")
=A0 io1.write(ioput)
end

when i open /home/pt/test/mtpback.txt,there is not what i want,where is
wrong?

At your favorite shell prompt:

$ cut -c 3- /home/pt/test/mtp.txt >| /home/pt/test/mtpback.txt

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
G

Gennady Bystritsky

how to delete the first two characters of each row?
i want to delete the first two characters of each row in file
/home/pt/test/mtp.txt
=20
=20
io=3Dopen("/home/pt/test/mtp.txt","r")
io1=3Dopen("/home/pt/test/mtpback.txt","w")
while line=3Dio.gets
ioput=3Dline.delete("/^..../")

As Brian has mentioned, String#delete in your code is instructed to remove =
the actual string starting with character '/', not a regular expression, as=
you might have meant. Also, most likely you want to exclude end-of-lines f=
rom counting.

Another problem is that your files remain open, so you need to close them a=
t some point. I think you need something like this:

File.open("/home/pt/test/mtpback.txt", "w") { |output|
File.open("/home/pt/test/mtp.txt", "r") { |input|
input.each { |line|
output.puts line.chomp[2..-1].to_s
}
}
}

Gennady.=
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

Another option, utilizing Ruby's mutable strings:

while line = io.gets
line.slice!(0, 2)
io1.write line
end
 
D

Daniel Berger

Another option, utilizing Ruby's mutable strings:

while line =3D io.gets
=A0 line.slice!(0, 2)
=A0 io1.write line
end

If you want to avoid temporary files:

# Backup your file first, just in case, for testing purposes.

require 'mmap'
m =3D Mmap.new('mtp.txt', 'rw')
m.gsub!(/^..(.*)/, '\1')
m.each{ |line| puts line }
m.unmap

Regards,

Dan
 
A

Albert Schlef

Robert said:
At your favorite shell prompt:

$ cut -c 3- /home/pt/test/mtp.txt >| /home/pt/test/mtpback.txt

Where is ">|" explained? Looks useful. I did "man bash", and I can find
about three mentions of ">|" there, but it's not explained.


(BTW, removing columns can be done in Vim too, it has a "visual block"
mode, which can be entered with Control-v.)
 
S

Seebs

Where is ">|" explained? Looks useful. I did "man bash", and I can find
about three mentions of ">|" there, but it's not explained.

If the redirection operator is >, and the noclobber
option to the set builtin has been enabled, the redirection
will fail if the file whose name results from the expansion
of word exists and is a regular file. If the redirection
operator is >|, or the redirection operator is > and the
noclobber option to the set builtin command is not enabled,
the redirection is attempted even if the file named by
word exists.

-s
 
R

Robert Klemme

Where is ">|" explained? Looks useful. I did "man bash", and I can find
about three mentions of ">|" there, but it's not explained.

This is even part of the POSIX standard. I don't have the link handy
and I believe you must register with them but reading and downloading
the standard is free.
(BTW, removing columns can be done in Vim too, it has a "visual block"
mode, which can be entered with Control-v.)

Cool, thanks for the hint! In vi(m) I would probably have used
":%s/^..//". :)

Kind regards

robert
 

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

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top