# Help processing a file or array

**URL:** <https://www.ruby-forum.com/t/help-processing-a-file-or-array/89740>\
**Category:** Ruby\
**Created:** [March 22, 2007, 1:30pm UTC](https://www.ruby-forum.com/t/help-processing-a-file-or-array/89740 "2007-03-22T13:30:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eduardo\_YSSSSSSSSez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ba9def/32.png) [@Eduardo\_YSSSSSSSSez](https://www.ruby-forum.com/u/Eduardo_YSSSSSSSSez)\
**Post date:** [March 22, 2007, 1:30pm UTC](https://www.ruby-forum.com/t/help-processing-a-file-or-array/89740/1 "2007-03-22T13:30:46Z")

</div>

Hello, I’m a newbie, and need some help to process a file.  
The file has lines with something like this:

rewrewrwer rrrrrrrrrrrrr aa1 rrrrrrrrrr  
rewrwerwer rrrrrrrrrrrrr bb1 rrrrrrrrrr  
rwerfwdffsd rrrrrrrrrrrrr cc1 rrrrrrrrrr  
ewrwerwerwer rrrrrrrrrrrrr dd1 rrrrrrrrrr  
trtretertert rrrrrrrrrrrrr ee1 rrrrrrrrrr

and another file with

aa1  
cc1

I’d like to create a new file without lines containing aa1 and cc1

Reading the files and get arrays with the content is easy:

lines = File.new(“file1”).readlines  
tags = File.new("file2).readlines

Is there a Ruby way to remove lines from ‘lines’ variable which contain  
tags from ‘tags’ variable?

---

<div class="post-metadata">

**Author:** ![Eduardo\_YSSSSSSSSez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ba9def/32.png) [@Eduardo\_YSSSSSSSSez](https://www.ruby-forum.com/u/Eduardo_YSSSSSSSSez)\
**Post date:** [March 22, 2007, 1:44pm UTC](https://www.ruby-forum.com/t/help-processing-a-file-or-array/89740/2 "2007-03-22T13:44:48Z")

</div>

You’d want to use a regular expression, I think. Probably a nested  
loop. I could do this in Perl in about two minutes, but I’m still  
adjusting my thinking for Ruby.

lines.each do |line|  
tags.each do |tag|  
final \<\< line if line !~ /#{tag.chop}/  
end  
end

Then write the “final” array to whatever file you want to.

I threw in the “.chop” there to get rid of the newline character on the  
tag.

-Augie

---

<div class="post-metadata">

**Author:** ![Eduardo\_YSSSSSSSSez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ba9def/32.png) [@Eduardo\_YSSSSSSSSez](https://www.ruby-forum.com/u/Eduardo_YSSSSSSSSez)\
**Post date:** [March 22, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/help-processing-a-file-or-array/89740/3 "2007-03-22T14:00:26Z")

</div>

On 3/22/07, Eduardo Yáñez Parareda [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Is there a Ruby way to remove lines from ‘lines’ variable which contain tags from ‘tags’ variable?
> 
> –  
> Eduardo Yáñez Parareda  
> [http://legalizate.blogspot.com](http://legalizate.blogspot.com)

tags\_re = Regexp.new(“\b(?:#{tags.map {|t|  
Regexp.escape(t.chomp)}.join(”|“)})\b”)  
lines.delete\_if {|l| l =~ tags\_re }

explanation:

tags\_re will have form “\b(?:tag1|tag2|tag3|…|tagn)\b”  
\b are to match only whole words, not part of the words.  
note that if you put in too many tags you may get errors (regexp too  
long or something similar)

the last line deletes all lines that match the regexp.

---

<div class="post-metadata">

**Author:** ![Eduardo\_YSSSSSSSSez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ba9def/32.png) [@Eduardo\_YSSSSSSSSez](https://www.ruby-forum.com/u/Eduardo_YSSSSSSSSez)\
**Post date:** [March 22, 2007, 3:55pm UTC](https://www.ruby-forum.com/t/help-processing-a-file-or-array/89740/4 "2007-03-22T15:55:25Z")

</div>

> tags\_re = Regexp.new("\b(?:#{tags.map {|t|  
> Regexp.escape(t.chomp)}.join("|")})\b")  
> lines.delete\_if {|l| l =~ tags\_re }

One more time I have to praise Ruby…  
Thanks Jan.

---

<div class="post-metadata">

**Author:** ![Eduardo\_YSSSSSSSSez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ba9def/32.png) [@Eduardo\_YSSSSSSSSez](https://www.ruby-forum.com/u/Eduardo_YSSSSSSSSez)\
**Post date:** [March 22, 2007, 5:43pm UTC](https://www.ruby-forum.com/t/help-processing-a-file-or-array/89740/5 "2007-03-22T17:43:03Z")

</div>

On 3/22/07, Eduardo Yáñez Parareda [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > tags\_re = Regexp.new(“\b(?:#{tags.map {|t|  
> > Regexp.escape(t.chomp)}.join(”|“)})\b”)  
> > lines.delete\_if {|l| l =~ tags\_re }
> 
> One more time I have to praise Ruby…  
> Thanks Jan.

Now that I look at it: this is more like perl than ruby… Ron’s  
version is probably a bit slower but much more readable…

---

<div class="post-metadata">

**Author:** ![Eduardo\_YSSSSSSSSez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ba9def/32.png) [@Eduardo\_YSSSSSSSSez](https://www.ruby-forum.com/u/Eduardo_YSSSSSSSSez)\
**Post date:** [March 22, 2007, 5:30pm UTC](https://www.ruby-forum.com/t/help-processing-a-file-or-array/89740/6 "2007-03-22T17:30:01Z")

</div>

On 3/22/07, Eduardo YÃ¡Ã±ez Parareda [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> lines = File.new(“file1”).readlines  
> tags = File.new("file2).readlines
> 
> Is there a Ruby way to remove lines from ‘lines’ variable which contain  
> tags from ‘tags’ variable?

The most straightforward way seems to be:

lines.reject do |line|  
tags.any? do |tag|  
line.include? tag.chomp  
end  
end

…or the same thing a bit more succinctly:

lines.reject {|line| tags.any? {|tag| line.include?(tag.chomp) }}

- chopper
