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?
Pen T. 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)
Pen T. 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
2010/4/13 Pen T. [email protected]:
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
Another option, utilizing Ruby’s mutable strings:
while line = io.gets
line.slice!(0, 2)
io1.write line
end
On Apr 13, 2010, at 4:17 AM, Pen T. 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("/^…/")
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, 11:26 am, Tony A. [email protected] wrote:
Another option, utilizing Ruby’s mutable strings:
while line = io.gets
line.slice!(0, 2)
io1.write line
end
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
Robert K. 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.)
On 2010-04-14, Albert S. [email protected] wrote:
Robert K. 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.
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 14.04.2010 03:06, Albert S. wrote:
Robert K. 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/^…//”.
Kind regards
robert