# File eliminating 'a'

**URL:** <https://www.ruby-forum.com/t/file-eliminating-a/222017>\
**Category:** Ruby\
**Created:** [August 9, 2012, 11:31am UTC](https://www.ruby-forum.com/t/file-eliminating-a/222017 "2012-08-09T11:31:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [August 9, 2012, 11:31am UTC](https://www.ruby-forum.com/t/file-eliminating-a/222017/1 "2012-08-09T11:31:46Z")

</div>

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”

Or simply how can I copy file character by character?  
I can modify then the program as my need.

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [August 9, 2012, 12:52pm UTC](https://www.ruby-forum.com/t/file-eliminating-a/222017/2 "2012-08-09T12:52:02Z")

</div>

On Thu, Aug 9, 2012 at 11:31 AM, ajay paswan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [August 9, 2012, 1:53pm UTC](https://www.ruby-forum.com/t/file-eliminating-a/222017/3 "2012-08-09T13:53:47Z")

</div>

Could not understand any line…  
can we do in conventional way?  
like open a file to read and then open a file to write and then write  
logically, then delete source and rename the new file.

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [August 9, 2012, 2:23pm UTC](https://www.ruby-forum.com/t/file-eliminating-a/222017/4 "2012-08-09T14:23:23Z")

</div>

ajay paswan wrote in post #1071764:

> 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”
> 
> Or simply how can I copy file character by character?

IO#getc:

while ch = STDIN.getc  
print “\<#{ch}\>”  
end

But if your input is line-based, you’re probably better off processing a  
line at a time.

A simple approach would be to split on double-quote, and then remove ‘a’  
from every other field, then join them back together.

> > line = ‘“acb"gha"abc”’  
> > =\> “"acb"gha"abc"”  
> > a = line.split(‘"’)  
> > =\> [“”, “acb”, “gha”, “abc”]  
> > 0.step(a.size-1,2) { |i| a[i].gsub!(/a/,‘’) }  
> > =\> 0  
> > puts a.join(‘"’)  
> > "acb"gh"abc  
> > =\> nil

A bit of care is needed for the trailing double-quote though, because  
split doesn’t give you an empty field on the end. You could add a  
trailing space then trim it off again at the end.

---

<div class="post-metadata">

**Author:** ![proggrammer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@proggrammer](https://www.ruby-forum.com/u/proggrammer)\
**Post date:** [August 9, 2012, 2:53pm UTC](https://www.ruby-forum.com/t/file-eliminating-a/222017/5 "2012-08-09T14:53:37Z")

</div>

On Thu, Aug 9, 2012 at 1:53 PM, ajay paswan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Could not understand any line..  
> can we do in conventional way?  
> like open a file to read and then open a file to write and then write  
> logically, then delete source and rename the new file.

Like so?

14:50:41 ~$ ruby19 rrr.rb t1.txt  
14:50:45 ~$ diff -U5 t1.txt.bak t1.txt  
— t1.txt.bak 2012-08-09 12:47:35.043920600 +0200  
+++ t1.txt 2012-08-09 14:50:45.712765600 +0200  
@@ -1,11 +1,11 @@  
111111111  
-a  
+  
b  
-ab  
-ba  
-bab  
+b  
+b  
+bb  
“a”  
“b”  
“ab”  
“ba”  
“bab”  
14:50:47 ~$ cat -n rrr.rb  
1 ARGV.each do |f|  
2 bak = f + “.bak”  
3 File.rename f, bak  
4 File.open f, ‘w’ do |out|  
5 File.foreach bak do |line|  
6 out.puts line.split(/(“[^”]\*“)/).each {|s| /\A”/ =~ s or  
s.gsub!(/a/, ‘’)}.join  
7 end  
8 end  
9 end

Cheers

robert
