Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 7 May 2011 11:09:01 -0700
From:      Chip Camden <>
To:        FreeBSD <>
Subject:   Re: Comparing two lists
Message-ID:  <>
In-Reply-To: <20110507125645.GA46576@guilt.hydra>
References:  <> <20110507125645.GA46576@guilt.hydra>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help

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=
> > exist in both files are not necessarily in the same location. Now I nee=
> > 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"foo.txt") {|l| l.chomp}; \
>     bar =3D"bar.txt") {|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.
> --=20
> Chad Perrin [ original content licensed OWL: ]

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

     ruby -e 'puts"foo.txt") {|l| l.chomp} & \"bar.txt") {|l| l.chomp }'

And to comply with DRY:

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

=2EO. | Sterling (Chip) Camden      |
=2E.O | |
OOO | 2048R/D6DBAF91              |

Content-Type: application/pgp-signature
Content-Disposition: inline

Version: GnuPG v1.4.11 (FreeBSD)



Want to link to this message? Use this URL: <>