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
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 from counting.
Another problem is that your files remain open, so you need to close them at some point. I think you need something like this:
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
···
On 2010-04-14, Albert Schlef <albertschlef@gmail.com> wrote:
Where is ">|" explained? Looks useful. I did "man bash", and I can find
about three mentions of ">|" there, but it's not explained.
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net | Seebs.Net <-- lawsuits, religion, and funny pictures Fair game (Scientology) - Wikipedia <-- get educated!
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/^..//".