From: Alex Young Date: 2007-11-29T01:39:13+09:00 Subject: Re: Deleting Text From a File woodyee wrote: > On Nov 28, 9:44 am, Alex Young wrote: >> woodyee wrote: >>> Hi! I had to manually remove decimals and tildes from a text file. How >>> could I have done this via programming? I googled this but didn't >>> really find anything, just stuff on removing spaces. Basically, I'd >>> like to be able to search a file and remove certain items (ex - all >>> decimals and tildes). Thanks! >> Sounds like a job for regular expressions. Have you got a short before >> and after example? >> >> -- >> Alex > > Sure. See below. Basically, the entire document was like this. To me, > Reg Exp's were the key but they're a weakness of mine so I was > lost. :-) > > > Original > --------- > 89.00~~~~ > 6330.00~15~CAT1~0005~1.00 > > Modified > ---------- > 89 > 6330 15 CAT1 0005 1 > irb(main):001:0> file = "89.00~~~~ irb(main):002:0" 6330.00~15~CAT1~0005~1.00 irb(main):003:0" irb(main):004:0" " => "89.00~~~~\n6330.00~15~CAT1~0005~1.00\n\n" irb(main):005:0> file.gsub(/\.\d+/, '').tr('~', ' ') => "89 \n6330 15 CAT1 0005 1\n\n" irb(main):006:0> puts _ 89 6330 15 CAT1 0005 1 I think that should do it :-) I don't think it's feasible to do it all in a single pass, which is why I've gone for separate gsub and tr phases. -- Alex