How to delete the first two characters of each row?

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?

···

--
Posted via http://www.ruby-forum.com/.

Pen Ttt wrote:

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

···

--
Posted via http://www.ruby-forum.com/\.

Pen Ttt wrote:

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)

···

--
Posted via http://www.ruby-forum.com/\.

At your favorite shell prompt:

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

Kind regards

robert

···

2010/4/13 Pen Ttt <myocean135@yahoo.cn>:

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?

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

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:

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.

···

On Apr 13, 2010, at 4:17 AM, Pen Ttt wrote:

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("/^..../")

Another option, utilizing Ruby's mutable strings:

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

···

On Tue, Apr 13, 2010 at 5:17 AM, Pen Ttt <myocean135@yahoo.cn> wrote:

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?
--
Posted via http://www.ruby-forum.com/\.

--
Tony Arcieri
Medioh! A Kudelski Brand

If you want to avoid temporary files:

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

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

Regards,

Dan

···

On Apr 13, 11:26 am, Tony Arcieri <tony.arci...@medioh.com> wrote:

Another option, utilizing Ruby's mutable strings:

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

Robert Klemme wrote:

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

···

--
Posted via http://www.ruby-forum.com/\.

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:

Robert Klemme wrote:

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.

--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
| Seebs.Net <-- lawsuits, religion, and funny pictures
Fair game (Scientology) - Wikipedia <-- get educated!

Robert Klemme wrote:

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.

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/^..//". :slight_smile:

Kind regards

  robert

···

On 14.04.2010 03:06, Albert Schlef wrote:

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