John Joyce
10/17/2007 4:06:00 AM
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'