Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 7 May 2011 11:09:01 -0700
From:      Chip Camden <sterling@camdensoftware.com>
To:        FreeBSD <freebsd-questions@freebsd.org>
Subject:   Re: Comparing two lists
Message-ID:  <20110507180901.GB76440@libertas.local.camdensoftware.com>
In-Reply-To: <20110507125645.GA46576@guilt.hydra>
References:  <4DC48DB6.8030907@lazlarlyricon.com> <20110507125645.GA46576@guilt.hydra>

next in thread | previous in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
Quoth Chad Perrin on Saturday, 07 May 2011:
> On Sat, May 07, 2011 at 02:09:26AM +0200, Rolf Nielsen wrote:
> > 
> > I have two text files, quite extensive ones. They have some lines in 
> > common and some lines are unique to one of the files. The lines that do 
> > exist in both files are not necessarily in the same location. Now I need 
> > to compare the files and output a list of lines that exist in both 
> > files. Is there a simple way to do this? diff? awk? sed? cmp? Or a 
> > combination of two or more of them?
> 
> Disclaimer:
> 
> This should probably be done with Unix command line utilities, and most
> likely by way of comm, as others explain here.  On the other hand, the
> others explaining that have done an admirable job of giving you some
> pretty comprehensive advice on that front before I got here, so I'll give
> you an alternative approach that is probably *not* how you should do it.
> 
> Alternative Approach:
> 
> You could always use a programming language reasonably well-suited to
> admin scripting.  The following is a one-liner in Ruby.
> 
>     ruby -e 'foo = File.open("foo.txt").readlines.map {|l| l.chomp}; \
>     bar = File.open("bar.txt").readlines.map {|l| l.chomp }; \
>     foo.each {|num| puts num if bar.include? num }'
> 
> Okay, so I'm kinda stretching the definition of "one-liner" if I'm
> using semicolons and escaping newlines.  If you really want to cram it
> all into one line of code, you could do something like replace the
> semicolons (and newline escapes) with the "and" keyword in each case.
> 
>     http://pastebin.com/nPR42760
> 
> -- 
> Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]


You could even just output the intersection of the two lists:

     ruby -e 'puts File.open("foo.txt").readlines.map {|l| l.chomp} & \
     File.open("bar.txt").readlines.map {|l| l.chomp }'

And to comply with DRY:

     ruby -e 'def fl(f) File.open(f).readlines.map {|l| l.chomp}; end; \
     puts fl("foo.txt") & fl("bar.txt")'

-- 
.O. | Sterling (Chip) Camden      | http://camdensoftware.com
..O | sterling@camdensoftware.com | http://chipsquips.com
OOO | 2048R/D6DBAF91              | http://chipstips.com

[-- Attachment #2 --]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (FreeBSD)

iQEcBAEBAgAGBQJNxYq9AAoJEIpckszW26+R5cQH/1bDfmmkNJ4CtbNkdxi+S1ls
8qQaYbw9kihlwSgAjAC91EhnGx5j5Ts8mfQix/cec0BdMztJjUnj+kEz7iOppJR9
bDrc4F8ICyrB+2hzQqPt+ho6MfaWkzezXLP/zTUHfkqAYJ7goJurZqusRfXNgOF7
jYSYQR5UvRYGOvsk7XVHdb/+c4sG/wrJGORe+njP6Y6lIXhGO02S9UFYeaNJ9/SE
IPW6klpt66MVD23VW0R4CofToG2EXKS6l4WbL2xa0pGNcyC7cJC32iA9FHNZD6Gc
kjQCp/ARosQYYk3cH1fTHihg5YVmnA73w1EqNeLI00Awaqde0oRsU/d+ebywg68=
=JWvz
-----END PGP SIGNATURE-----

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20110507180901.GB76440>