File eliminating 'a'

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.

On Thu, Aug 9, 2012 at 11:31 AM, ajay paswan [email protected]
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

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.

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.

On Thu, Aug 9, 2012 at 1:53 PM, ajay paswan [email protected]
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs