[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to remove "~" in those files

Vidya Vidya

10/16/2007 12:59:00 PM



Hi all,

How to remove at end of the record "~", i am trying this, but i
couldn't remove, please let me know if any body knows.
for example , i have 2 files name sample1, sample2,

for sample1 records:(this is one file)

LIN*EDIA000005*000000570*570~
LIN*EDIA000006*000000570*570~
LIN*EDIA000007*000000570*570~ // here i want to remove ~ in the looping
procees

sample 2:


LIN*EDIA000008*000000570*570~
LIN*EDIA000009*000000570*570~
LIN*EDIA000002*000000570*570~ // here i want to remove ~ in the looping
procees
--
Posted via http://www.ruby-....

12 Answers

Shai Rosenfeld

10/16/2007 1:13:00 PM

0

> for sample1 records:(this is one file)
>
> LIN*EDIA000005*000000570*570~
> LIN*EDIA000006*000000570*570~
> LIN*EDIA000007*000000570*570~ // here i want to remove ~ in the looping
> procees
>

use gsub ( http://www.ruby-doc.org/core/classes/String.ht... )
with a pattern or whatever will suit u'r needs
--
Posted via http://www.ruby-....

Ben Giddings

10/17/2007 1:51:00 AM

0

On Oct 16, 2007, at 08:59, Vidya Vidya wrote:
> for sample1 records:(this is one file)
>
> LIN*EDIA000005*000000570*570~
> LIN*EDIA000006*000000570*570~
> LIN*EDIA000007*000000570*570~ // here i want to remove ~ in the
> looping
> procees
>
> sample 2:
>
>
> LIN*EDIA000008*000000570*570~
> LIN*EDIA000009*000000570*570~
> LIN*EDIA000002*000000570*570~ // here i want to remove ~ in the
> looping
> procees

Do you want to remove all the tilde (~) characters, or just the last
one before "procees"?

Do you want the output to be like the following?

LIN*EDIA000005*000000570*570~
LIN*EDIA000006*000000570*570~
LIN*EDIA000007*000000570*570
procees

If so, and if you have enough space for two of the files, the easiest
way is probably to open an "output" file and read from the input
file, but read ahead slightly, so you know when you're at the next
record, something like:

prev_record = nil
File.open("output.txt", "w") do |out|
File.foreach("records.txt") do |record|
if prev_record
if /procees/.match(line)
out.puts prev_record.gsub(/~$/, "")
else
out.puts prev_record
end
end
prev_record = record
end
out.puts prev_record # since we were one-line behind at the end
end

FileUtils.mv("output.txt", "records.txt")

Ben


John Joyce

10/17/2007 4:06:00 AM

0


On Oct 16, 2007, at 8:50 PM, Ben Giddings wrote:

> On Oct 16, 2007, at 08:59, Vidya Vidya wrote:
>> for sample1 records:(this is one file)
>>
>> LIN*EDIA000005*000000570*570~
>> LIN*EDIA000006*000000570*570~
>> LIN*EDIA000007*000000570*570~ // here i want to remove ~ in the
>> looping
>> procees
>>
>> sample 2:
>>
>>
>> LIN*EDIA000008*000000570*570~
>> LIN*EDIA000009*000000570*570~
>> LIN*EDIA000002*000000570*570~ // here i want to remove ~ in the
>> looping
>> procees
>
> Do you want to remove all the tilde (~) characters, or just the
> last one before "procees"?
>
> Do you want the output to be like the following?
>
> LIN*EDIA000005*000000570*570~
> LIN*EDIA000006*000000570*570~
> LIN*EDIA000007*000000570*570
> procees
>
> If so, and if you have enough space for two of the files, the
> easiest way is probably to open an "output" file and read from the
> input file, but read ahead slightly, so you know when you're at the
> next record, something like:
>
> prev_record = nil
> File.open("output.txt", "w") do |out|
> File.foreach("records.txt") do |record|
> if prev_record
> if /procees/.match(line)
> out.puts prev_record.gsub(/~$/, "")
> else
> out.puts prev_record
> end
> end
> prev_record = record
> end
> out.puts prev_record # since we were one-line behind at the end
> end
>
> FileUtils.mv("output.txt", "records.txt")
>
> Ben
>
>
chop could be useful here.
2 chops and a +

as you iterate each line:
line.chop!.chop! + "\n"

(assuming you still want the newline on each line)

Of course this is a quicka and dirty approach, assuming that every
record ends with '~\n'



Ben Giddings

10/17/2007 4:13:00 AM

0

On Oct 17, 2007, at 00:05, John Joyce wrote:
> as you iterate each line:
> line.chop!.chop! + "\n"

You don't want to use chop! unless you're trying to modify line. If
you really wanted to use chop! you'd use it like:

line.chop!
line.chop!
line =+ "\n"

If you want to chain them, you probably want line.chop.chop + "\n".

In any case, it's safer to use the regexp because you don't know if
there's extra whitespace, what the end-of-line chars look like, etc.

Ben


John Joyce

10/17/2007 4:59:00 AM

0


On Oct 16, 2007, at 11:12 PM, Ben Giddings wrote:

> On Oct 17, 2007, at 00:05, John Joyce wrote:
>> as you iterate each line:
>> line.chop!.chop! + "\n"
>
> You don't want to use chop! unless you're trying to modify line.
> If you really wanted to use chop! you'd use it like:
>
> line.chop!
> line.chop!
> line =+ "\n"
>
> If you want to chain them, you probably want line.chop.chop + "\n".
>
> In any case, it's safer to use the regexp because you don't know if
> there's extra whitespace, what the end-of-line chars look like, etc.
>
> Ben
>
>
Uh, yeah, that's why you should test it.
Regex is great if you want to do all of that. Tend to be more intensive.

Pretty easy to determine EOL characters... that's what's determining
the lines already, OP did refer to lines as 'records'

OP wants to modify the lines.

Lines of something like a log file will be consistent..
but like I said, quick and dirty.

Vidya Vidya

10/17/2007 5:08:00 AM

0

thanks for reply, actually i want to remove only end of the last record
"~", to each file, here this is my coding, please let me, in this i
should remove the last "~" only.

Dir["#{dirname}/**/**"].each do | thisfile |
@m=@m+1
thisfile.gsub! ( /\// , '\\' )
results.push ( thisfile )
f = File.open(thisfile,"r")
@read_file=f.read()
@split_record=@read_file.split("~") // i am spiliting the
records
@split_record.each do |v|
@split_data=v.split("\*")
// here my inner process
end

end

but i want to remove last sysmbol at the end of the each file, if i
don't remove the "~" means , i couldn't get output as right, otherwise
if you have any idea please let me know
--
Posted via http://www.ruby-....

7stud --

10/17/2007 5:58:00 AM

0

Vidya Vidya wrote:
> thanks for reply, actually i want to remove only end of the last record
> "~", to each file, here this is my coding, please let me, in this i
> should remove the last "~" only.
>
> Dir["#{dirname}/**/**"].each do | thisfile |
> @m=@m+1
> thisfile.gsub! ( /\// , '\\' )
> results.push ( thisfile )
> f = File.open(thisfile,"r")
> @read_file=f.read()
> @split_record=@read_file.split("~")
>

The last line removes all "~" from the data, so there isn't a '~ ' on
the last line or any other line.

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

Vidya Vidya

10/17/2007 6:01:00 AM

0

sorry, i couldn't understood, what you are telling, could you pleas can
tell me clearly?
--
Posted via http://www.ruby-....

7stud --

10/17/2007 6:05:00 AM

0

7stud -- wrote:

This line of code:

>
>>@split_record=@read_file.split("~")
>>
>

removes all '~' characters from @read_file. So, when you ask how to
remove the '~' from the last line of your input, it doesn't make any
sense.

Are you asking how to rewrite the file itself, so that the last line
doesn't end in a '~'? If you want to rewrite the file just so that
split() will return different values, there is a 100.1% chance that
doing that is the wrong solution.
--
Posted via http://www.ruby-....

Vidya Vidya

10/17/2007 6:39:00 AM

0

However, i should remove the "~" at the end of the records, otherwise
its not storing in to the database properly, is there any way to avoid
"~"?



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