From: Ben Giddings Date: 2007-10-17T10:50:53+09:00 Subject: Re: How to remove "~" in those files 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