From: Robert Klemme Date: 2012-08-09T19:51:05+09:00 Subject: Re: File eliminating 'a' On Thu, Aug 9, 2012 at 11:31 AM, ajay paswan wrote: > How can I eliminate every 'a' in a file if it is not inside double > quote? > > For example: > > input:"acb"gha"abc" > output:"acb"gh"abc" Here's one way: $ ruby19 -i.bak rr.rb t1.txt t2.txt $ diff -U5 t1.txt.bak t1.txt --- t1.txt.bak 2012-08-09 12:47:35.043920600 +0200 +++ t1.txt 2012-08-09 12:50:02.181058200 +0200 @@ -1,11 +1,11 @@ 111111111 -a + b -ab -ba -bab +b +b +bb "a" "b" "ab" "ba" "bab" $ diff -U5 t2.txt.bak t2.txt --- t2.txt.bak 2012-08-09 12:47:55.923182500 +0200 +++ t2.txt 2012-08-09 12:50:02.184058800 +0200 @@ -1,11 +1,11 @@ 222222222 -a + b -ab -ba -bab +b +b +bb "a" "b" "ab" "ba" "bab" $ cat -n rr.rb 1 ARGF.each do |line| 2 puts line.split(/("[^"]*")/).each {|s| /\A"/ =~ s or s.gsub!(/a/, '')}.join 3 end Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/