From owner-freebsd-questions@FreeBSD.ORG Sat May 7 13:10:12 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 EA90D106566B for ; Sat, 7 May 2011 13:10:12 +0000 (UTC) (envelope-from perrin@apotheon.com) Received: from oproxy3-pub.bluehost.com (oproxy3-pub.bluehost.com [69.89.21.8]) by mx1.freebsd.org (Postfix) with SMTP id B61A38FC12 for ; Sat, 7 May 2011 13:10:12 +0000 (UTC) Received: (qmail 13120 invoked by uid 0); 7 May 2011 13:10:12 -0000 Received: from unknown (HELO box543.bluehost.com) (74.220.219.143) by oproxy3.bluehost.com with SMTP; 7 May 2011 13:10:12 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=apotheon.com; h=Date:From:To:Subject:Message-ID:Mail-Followup-To:References:Mime-Version:Content-Type:Content-Disposition:In-Reply-To:User-Agent:X-Identified-User; b=ioL1wtzNfz5/0jyeiF9Gtziql99777ifH9rmq4ewwL3Ajyk4opvx4sYihJCgUNDX3+ocjV/KC0OwtWDNlQXkbZDESGNxqBtRdPEVWE/Iy4JQN4L5kysNnpnasR/TlhTX; Received: from c-24-8-180-234.hsd1.co.comcast.net ([24.8.180.234] helo=kukaburra.hydra) by box543.bluehost.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1QIhGt-0006xR-2t for freebsd-questions@freebsd.org; Sat, 07 May 2011 07:10:12 -0600 Received: by kukaburra.hydra (sSMTP sendmail emulation); Sat, 07 May 2011 06:56:45 -0600 Date: Sat, 7 May 2011 06:56:45 -0600 From: Chad Perrin To: FreeBSD Message-ID: <20110507125645.GA46576@guilt.hydra> Mail-Followup-To: FreeBSD References: <4DC48DB6.8030907@lazlarlyricon.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="yrj/dFKFPuw6o+aM" Content-Disposition: inline In-Reply-To: <4DC48DB6.8030907@lazlarlyricon.com> User-Agent: Mutt/1.4.2.3i X-Identified-User: {2737:box543.bluehost.com:apotheon:apotheon.org} {sentby:smtp auth 24.8.180.234 authed with ren@apotheon.org} 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 13:10:13 -0000 --yrj/dFKFPuw6o+aM Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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 need= =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? 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 =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 }' 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 --=20 Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] --yrj/dFKFPuw6o+aM Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAk3FQY0ACgkQ9mn/Pj01uKW4RwCg79+pYycRhCaURcDCa0u9epJ/ mFUAoJfqHPyJ/B8SiwuLBs3SzADxe5nE =rNzZ -----END PGP SIGNATURE----- --yrj/dFKFPuw6o+aM--