From owner-freebsd-questions@FreeBSD.ORG Sat May 7 18:09:07 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id F1666106564A for ; Sat, 7 May 2011 18:09:06 +0000 (UTC) (envelope-from sterling@camdensoftware.com) Received: from wh1.interactivevillages.com (ca.2e.7bae.static.theplanet.com [174.123.46.202]) by mx1.freebsd.org (Postfix) with ESMTP id BB3808FC15 for ; Sat, 7 May 2011 18:09:06 +0000 (UTC) Received: from c-24-22-230-24.hsd1.wa.comcast.net ([24.22.230.24] helo=_HOSTNAME_) by wh1.interactivevillages.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1QIlw7-0005Lr-KP for freebsd-questions@freebsd.org; Sat, 07 May 2011 11:08:40 -0700 Received: by _HOSTNAME_ (sSMTP sendmail emulation); Sat, 07 May 2011 11:09:01 -0700 Date: Sat, 7 May 2011 11:09:01 -0700 From: Chip Camden To: FreeBSD Message-ID: <20110507180901.GB76440@libertas.local.camdensoftware.com> Mail-Followup-To: FreeBSD References: <4DC48DB6.8030907@lazlarlyricon.com> <20110507125645.GA46576@guilt.hydra> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="EuxKj2iCbKjpUGkD" Content-Disposition: inline In-Reply-To: <20110507125645.GA46576@guilt.hydra> User-Agent: Mutt/1.4.2.3i Company: Camden Software Consulting URL: http://camdensoftware.com X-PGP-Key: http://pgp.mit.edu:11371/pks/lookup?search=0xD6DBAF91 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - wh1.interactivevillages.com X-AntiAbuse: Original Domain - freebsd.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - camdensoftware.com X-Source: X-Source-Args: X-Source-Dir: Subject: Re: Comparing two lists X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 07 May 2011 18:09:07 -0000 --EuxKj2iCbKjpUGkD Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Quoth Chad Perrin on Saturday, 07 May 2011: > On Sat, May 07, 2011 at 02:09:26AM +0200, Rolf Nielsen wrote: > >=20 > > I have two text files, quite extensive ones. They have some lines in=20 > > common and some lines are unique to one of the files. The lines that do= =20 > > exist in both files are not necessarily in the same location. Now I nee= d=20 > > to compare the files and output a list of lines that exist in both=20 > > files. Is there a simple way to do this? diff? awk? sed? cmp? Or a=20 > > combination of two or more of them? >=20 > Disclaimer: >=20 > 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. >=20 > Alternative Approach: >=20 > You could always use a programming language reasonably well-suited to > admin scripting. The following is a one-liner in Ruby. >=20 > ruby -e 'foo =3D File.open("foo.txt").readlines.map {|l| l.chomp}; \ > bar =3D File.open("bar.txt").readlines.map {|l| l.chomp }; \ > foo.each {|num| puts num if bar.include? num }' >=20 > 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. >=20 > http://pastebin.com/nPR42760 >=20 > --=20 > 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")' --=20 =2EO. | Sterling (Chip) Camden | http://camdensoftware.com =2E.O | sterling@camdensoftware.com | http://chipsquips.com OOO | 2048R/D6DBAF91 | http://chipstips.com --EuxKj2iCbKjpUGkD Content-Type: application/pgp-signature Content-Disposition: inline -----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----- --EuxKj2iCbKjpUGkD--