Home > bash > Set difference between two files

Set difference between two files

Problem
You have two text files, short.txt and long.txt, and long.txt contains some extra lines. Which are these lines? That is, you want to perform a set difference: L \ S.

Solution
You can do this with the “comm” command:

First, make sure that the two files are sorted. Then:

comm -23 long_sorted.txt short_sorted.txt

Example

$ cat as.txt 
sl-1215
sl-2112
sl-9023
sl-9029
$ 
$ cat bs.txt 
sl-1215
sl-2112
sl-9012
sl-9016
sl-9023
sl-9029
$ 
$ comm -23 bs.txt as.txt 
sl-9012
sl-9016
$

Find more examples on “commhere (like union, etc.)

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: