# How to put unique lines from regexped file

**URL:** <https://www.ruby-forum.com/t/how-to-put-unique-lines-from-regexped-file/180493>\
**Category:** Ruby\
**Created:** [December 20, 2009, 12:40pm UTC](https://www.ruby-forum.com/t/how-to-put-unique-lines-from-regexped-file/180493 "2009-12-20T12:40:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 20, 2009, 12:40pm UTC](https://www.ruby-forum.com/t/how-to-put-unique-lines-from-regexped-file/180493/1 "2009-12-20T12:40:13Z")

</div>

Hi all,

I have such a question, when i open a file doing some grep stuff…

* * *

## File.open(‘aa.xml’).each do |line| dev\_name = ARGV[1] if line =~ /prf:CcppAccept/…line =~ /\</prf:CcppAccept\>/ if line =~ /rdf:li(image|audio|video).\*\</rdf:li\>/ line.gsub(‘rdf:liimage/jpeg’, “INSERT INTO VMPOLICY\_VALUES VALUES(’#{dev\_name}’,‘jpginpage’,‘true’,’#{$ver}’)”)).gsub( “\</rdf:li\>”, “;”).each do |ss|

I recive output like

INSERT INTO TABLE VALUES(1,1,1)  
INSERT INTO TABLE VALUES(1,2,2)  
INSERT INTO TABLE VALUES(1,1,1)

now i wanted to put only that lines which are uniqe…  
INSERT INTO TABLE VALUES(1,1,1)  
INSERT INTO TABLE VALUES(1,2,2)

how i can do this ??

i thought about…

puts ss.unque

but its dont work

Please Help

Regards  
beny18241

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 20, 2009, 12:48pm UTC](https://www.ruby-forum.com/t/how-to-put-unique-lines-from-regexped-file/180493/2 "2009-12-20T12:48:35Z")

</div>

beny 18241 wrote:

> Hi all,
> 
> I have such a question, when i open a file doing some grep stuff…
> 
> * * *
> 
> ## File.open(‘aa.xml’).each do |line| dev\_name = ARGV[1] if line =~ /prf:CcppAccept/…line =~ /\</prf:CcppAccept\>/ if line =~ /rdf:li(image|audio|video).\*\</rdf:li\>/ line.gsub(‘rdf:liimage/jpeg’, “INSERT INTO VMPOLICY\_VALUES VALUES(’#{dev\_name}’,‘jpginpage’,‘true’,’#{$ver}’)”)).gsub( “\</rdf:li\>”, “;”).each do |ss|
> 
> I recive output like
> 
> INSERT INTO TABLE VALUES(1,1,1)  
> INSERT INTO TABLE VALUES(1,2,2)  
> INSERT INTO TABLE VALUES(1,1,1)
> 
> now i wanted to put only that lines which are uniqe…  
> INSERT INTO TABLE VALUES(1,1,1)  
> INSERT INTO TABLE VALUES(1,2,2)
> 
> how i can do this ??
> 
> i thought about…
> 
> puts ss.unque
> 
> but its dont work
> 
> Please Help
> 
> Regards  
> beny18241

ok i figure it out 🙂

puts ss unless ss == @prev; @prev = ss

cheers

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 20, 2009, 1:50pm UTC](https://www.ruby-forum.com/t/how-to-put-unique-lines-from-regexped-file/180493/3 "2009-12-20T13:50:50Z")

</div>

On 20.12.2009 12:50, beny 18241 wrote:

> > ```
> > line.gsub('<rdf:li>image/jpeg', "INSERT INTO VMPOLICY_VALUES
> > 
> > ```
> > 
> > now i wanted to put only that lines which are uniqe…  
> > but its dont work  
> > puts ss unless ss == @prev; @prev = ss  
> > That works only if the input is ordered. For unordered input here’s an  
> > efficient way:

require ‘set’

unique = Set.new

File.foreach(‘aa.xml’) do |line|  
x = … line …  
puts x if unique.add? x  
end

Of course you can also use a Hash for this.

unique = {}

File.foreach(‘aa.xml’) do |line|  
x = … line …

unique.fetch x do  
unique[x] = true  
puts x  
end  
end

Btw, since your input appears to be XML it’s probably a better idea to  
process it with XML tools like REXML and the like.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 20, 2009, 3:25pm UTC](https://www.ruby-forum.com/t/how-to-put-unique-lines-from-regexped-file/180493/4 "2009-12-20T15:25:39Z")

</div>

Robert K. wrote:

> require ‘set’
> 
> unique = Set.new
> 
> File.foreach(‘aa.xml’) do |line|  
> x = … line …  
> puts x if unique.add? x  
> end

That’s fine for the plodding proles, but we cossacks always shout the  
battle-cry: “We don’t need no stinkin’ loops!”

puts IO.readlines(‘aa.xml’).map{|s| s.sub( … )}.uniq

Everything must be done in one fell swoop or not at all!

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 20, 2009, 3:40pm UTC](https://www.ruby-forum.com/t/how-to-put-unique-lines-from-regexped-file/180493/5 "2009-12-20T15:40:52Z")

</div>

On 20.12.2009 15:40, W. James wrote:

> That’s fine for the plodding proles, but we cossacks always shout the  
> battle-cry: “We don’t need no stinkin’ loops!”
> 
> puts IO.readlines(‘aa.xml’).map{|s| s.sub( … )}.uniq
> 
> Everything must be done in one fell swoop or not at all!

Strong words, but I hope you are aware that this, taken as a general  
rule, is bad advice.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![beny18241](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbce88/32.png) [@beny18241](https://www.ruby-forum.com/u/beny18241)\
**Post date:** [December 20, 2009, 8:04pm UTC](https://www.ruby-forum.com/t/how-to-put-unique-lines-from-regexped-file/180493/6 "2009-12-20T20:04:41Z")

</div>

Robert K. wrote:

> On 20.12.2009 15:40, W. James wrote:
> 
> > That’s fine for the plodding proles, but we cossacks always shout the  
> > battle-cry: “We don’t need no stinkin’ loops!”
> > 
> > puts IO.readlines(‘aa.xml’).map{|s| s.sub( … )}.uniq
> > 
> > Everything must be done in one fell swoop or not at all!
> 
> Strong words, but I hope you are aware that this, taken as a general  
> rule, is bad advice.

@geekdom = {@everything.reject {|e| !e.fell\_swoop?}}.good\_advice? ?  
:alive : :dead

🙂

> Cheers
> 
> robert

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalidÂ
