# Remove specific text from line

**URL:** <https://www.ruby-forum.com/t/remove-specific-text-from-line/177176>\
**Category:** Ruby\
**Created:** [October 12, 2009, 9:27pm UTC](https://www.ruby-forum.com/t/remove-specific-text-from-line/177176 "2009-10-12T21:27:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [October 12, 2009, 9:27pm UTC](https://www.ruby-forum.com/t/remove-specific-text-from-line/177176/1 "2009-10-12T21:27:07Z")

</div>

Hi  
thanks for all the help on my previous question, but now I am trying to  
clean up the output of some text output.

currently I have:  
Time@110.075: EVENT: [LOG] \*\*\* DEBUG ACTION \*\*\*

and I’d like it to have:  
110.075:DEBUG ACTION

: just for easy import into excel.

Here is the current ruby file I am working with where is looked for  
lines with DEBUG and START in them.

File.open(‘output.txt’, ‘w’) do |f2|  
File.readlines(“original.txt”).each do |line|  
if line =~ /DEBUG/ || line =~ /START/  
f2.puts line  
end  
end  
end

Any ideas?

thanks for your help 🙂

---

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [October 12, 2009, 9:32pm UTC](https://www.ruby-forum.com/t/remove-specific-text-from-line/177176/2 "2009-10-12T21:32:51Z")

</div>

On Mon, Oct 12, 2009 at 3:27 PM, Collin M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> : just for easy import into excel.  
> end
> 
> Any ideas?
> 
> ## thanks for your help 🙂
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

why don’t you gsub(‘Time@’, ‘’).gsub(’ EVENT: [LOG] \*\*\* ‘, ‘’).gsub(’  
\*\*\*',  
‘’)

Greg

---

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [October 12, 2009, 9:43pm UTC](https://www.ruby-forum.com/t/remove-specific-text-from-line/177176/3 "2009-10-12T21:43:58Z")

</div>

On 10/12/09, Collin M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> : just for easy import into excel.  
> end
> 
> Any ideas?

You want a regexp. Something like this should work. (Stick it in  
before the puts line.) (You may need to customize depending on your  
exact requirements.)

line.sub!(/^Time@ (\d+.\d+):.\*\*{3} ([A-Z]+ [A-Z]+) \*{3}$/){ $1+‘:’+$2  
}

---

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [October 12, 2009, 9:51pm UTC](https://www.ruby-forum.com/t/remove-specific-text-from-line/177176/4 "2009-10-12T21:51:35Z")

</div>

Hi!

I think you can use the following solution:

class TestClass  
def test(val)  
val.scan(/^[^@]+@(\d{1,3}.\d{1,3})[^_]+\*{3}\s_([^\*]+)/) do  
puts $1+" : "+$2  
end  
end  
end

myInst=TestClass.new  
myInst.test(“Time@110.075: EVENT: [LOG] \*\*\* DEBUG ACTION \*\*\*”);

The result is: 110.075 : DEBUG ACTION

---

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [October 13, 2009, 8:15am UTC](https://www.ruby-forum.com/t/remove-specific-text-from-line/177176/5 "2009-10-13T08:15:37Z")

</div>

It’s because I put line myInst.test(“Time@110.075: EVENT: [LOG] \*\*\*  
DEBUG ACTION \*\*\*”);  
There’s string literal: “Time@110.075: EVENT: [LOG] \*\*\* DEBUG ACTION  
\*\*\*”  
If you want parse file content you must read lines and put it to method:

File.open(“fname.txt”).each do |line|  
myInst.test(line)  
end

---

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [October 13, 2009, 11:48pm UTC](https://www.ruby-forum.com/t/remove-specific-text-from-line/177176/6 "2009-10-13T23:48:05Z")

</div>

Caleb C. wrote:

> On 10/12/09, Collin M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > : just for easy import into excel.  
> > end
> > 
> > Any ideas?
> 
> You want a regexp. Something like this should work. (Stick it in  
> before the puts line.) (You may need to customize depending on your  
> exact requirements.)
> 
> line.sub!(/^Time@ (\d+.\d+):.\*\*{3} ([A-Z]+ [A-Z]+) \*{3}$/){ $1+‘:’+$2  
> }

I tried this with

File.open(‘output.txt’, ‘w’) do |f2|  
File.readlines(“original.txt”).each do |line|  
if line =~ /DEBUG/ || line =~ /START/  
f2.puts line.sub!(/^Time@ (\d+.\d+):.\*\*{3} ([A-Z]+ [A-Z]+)  
\*{3}$/){ $1+‘:’+$2 }  
end

end  
end

and I get the output.txt with nil for each positive match.

---

<div class="post-metadata">

**Author:** ![cidol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/54ee81/32.png) [@cidol](https://www.ruby-forum.com/u/cidol)\
**Post date:** [October 12, 2009, 11:09pm UTC](https://www.ruby-forum.com/t/remove-specific-text-from-line/177176/7 "2009-10-12T23:09:29Z")

</div>

Alexey B. wrote:

> Hi!
> 
> I think you can use the following solution:
> 
> class TestClass  
> def test(val)  
> val.scan(/^[^@]+@(\d{1,3}.\d{1,3})[^_]+\*{3}\s_([^\*]+)/) do  
> puts $1+" : "+$2  
> end  
> end  
> end
> 
> myInst=TestClass.new  
> myInst.test(“Time@110.075: EVENT: [LOG] \*\*\* DEBUG ACTION \*\*\*”);
> 
> The result is: 110.075 : DEBUG ACTION

I tried this and 110.075 : DEBUG ACTION repeats for all the lines in the  
output.
