Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6932333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:41:33+00:00 2026-05-27T11:41:33+00:00

I sometimes need to compare two text files. Obviously, diff shows the differences, it

  • 0

I sometimes need to compare two text files. Obviously, diff shows the differences, it also hides the similarities, which is kind of the point.

Suppose I want to do other comparisons on these files: set union, intersection, and subtraction, treating each line as an element in the set.

Are there similarly simple common utilities or one-liners which can do this?


Examples:

a.txt

john
mary

b.txt

adam
john

$> set_union a.txt b.txt
john
mary
adam

$> set_intersection a.txt b.txt
john

$> set_difference a.txt b.txt
mary
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:41:34+00:00Added an answer on May 27, 2026 at 11:41 am

    Union: sort -u files…

    Intersection: sort files… | uniq -d

    Overall difference (elements which are just in one of the files):
    sort files… | uniq -u

    Mathematical difference (elements only once in one of the files):
    sort files… | uinq -u | sort - <(sort -u fileX ) | uniq -d

    The first two commands get me all unique elements. Then we merge this with the file we’re interested in. Command breakdown for sort - <(sort -u fileX ):

    The - will process stdin (i.e. the list of all unique elements).

    <(...) runs a command, writes the output in a temporary file and passes the path to the file to the command.

    So this gives is a mix of all unique elements plus all unique elements in fileX. The duplicates are then the unique elements which are only in fileX.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a diff/compare tool that shows differing lines from two text files,
Sometimes I need to quickly extract some arbitrary data from XML files to put
I sometimes need two string variables, where the value of the second one is
I've got a panel which will sometimes need more vertical screen space than naturally
Sometimes I need to insert some similar lines in a file which differ only
I sometimes need to use Visual Studio when I have limited screen real estate
I sometimes need to look for information for a special portion of code. When
I sometimes need to debug JS in other browsers, and it would be really
I sometimes need to run gacutil.exe or installutil.exe, etc. from the command line. Is
In my app I sometimes need to rebuild and repopulate database file. SQLite databse

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.