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

  • Home
  • SEARCH
  • 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 7046291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:37:58+00:00 2026-05-28T02:37:58+00:00

I’d like to join two files in bash using a common column. I want

  • 0

I’d like to join two files in bash using a common column. I want to retain both all pairable and unpairable lines from both files. Unfortunately using join I could save unpairable fields from only one file, eg. join -1 1 -2 2 -a1 -t" ".
I’d also want to retain all pairings for repeated entries (in join column) from both files.
I.e. If file1 is
x id1 a b
x id1 c d
x id1 d f
x id2 c x
x id3 f v

and second file is

id1 df cf
id1 ds dg
id2 cv df
id2 as ds
id3 cf cg

the resulting file should be:

x id1 a b df cf
x id1 a b ds dg
x id1 c d df cf
x id1 c d ds dg
x id1 d f df cf
x id1 d f ds dg
x id2 c x cv df
x id2 c x as ds
x id3 f v cf cg

That’s why I’ve always using SAS to make such join, after sorting appropriate columns.

data x;
merge file1 file2;
by common_column;
run;

It works fine but
1. as I use Ubuntu for most time I have to switch to Windows to merge data in SAS.
2. most importantly, SAS can truncate too long data entries.

That’s why I’d prefer to join my files in bash, but I don’t know appropriate command.
Could someone help me, or direct me to appropriate resource?

  • 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-28T02:37:59+00:00Added an answer on May 28, 2026 at 2:37 am

    According to join‘s man page, -a <filenum> retains all unpairable lines from file <filenum> (1 or 2). So, just add -a1 -a2 to your command line and you should be done. For example:

    # cat a
    1 blah
    2 foo
    
    # cat b
    2 bar
    3 baz
    
    # join -1 1 -2 1 -t" " a b
    2 foo bar
    
    # join -1 1 -2 1 -t" " -a1 a b
    1 blah
    2 foo bar
    
    # join -1 1 -2 1 -t" " -a2 a b
    2 foo bar
    3 baz
    
    # join -1 1 -2 1 -t" " -a1 -a2 a b
    1 blah
    2 foo bar
    3 baz
    

    Is this what you were looking for?

    Edit:

    Since you provided more detail, here is how to produce your desired output (note that my file a is your first file and my file b your second file. I had to reverse -1 1 -2 2 to -1 2 -2 1 to join on the id). I added a field list to format the output as well – note that ‘0’ is the join field in it:

    # join -1 2 -2 1 -o 1.1,0,1.3,1.4,2.2,2.3 a b
    

    produces what you’ve given. Add -a1 -a2 to retain unpairable lines from both files you then get two more lines (you can guess my test data from them):

    x id4 u t
     id5   ui oi
    

    Which is rather unreadable since any left out field is just a space. So let’s replace them with a ‘-‘, leading to:

    # join -1 2 -2 1 -a1 -a2 -e- -o 1.1,0,1.3,1.4,2.2,2.3 a b
    x id1 a b df cf
    x id1 a b ds dg
    x id1 c d df cf
    x id1 c d ds dg
    x id1 d f df cf
    x id1 d f ds dg
    x id2 c x cv df
    x id2 c x as ds
    x id3 f v cf cg
    x id4 u t - -
    - id5 - - ui oi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into

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.