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 9196189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:47:49+00:00 2026-06-17T21:47:49+00:00

I do have two files: File1 12 abc 34 cde 42 dfg 11 df

  • 0

I do have two files:

 File1

 12    abc
 34    cde
 42    dfg
 11    df
 9     e   


 File2

 23    abc
 24    gjr
 12    dfg
 8     df

I want to merge files column by column (if column 2 is the same) for the output like this:

  File1  File2
   12    23    abc
   42    12    dfg
   11    8     df
   34    NA    cde
   9     NA    e
   NA    24    gjr

How can I do this?

I tried it like this:

 cat File* >> tmp; sort tmp | uniq -c | awk '{print $2}' > column2; for i in
 $(cat column2); do grep -w "$i" File*

But this is where I am stuck…
Don’t know how after greping I should combine files column by column & write NA where value is missing.

Hope someone could help me with this.

  • 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-06-17T21:47:50+00:00Added an answer on June 17, 2026 at 9:47 pm

    Since I was testing with bash 3.2 running as sh (which does not have process substitution as sh), I used two temporary files to get the data ready for use with join:

    $ sort -k2b File2 > f2.sort
    $ sort -k2b File1 > f1.sort
    $ cat f1.sort
    12    abc
    34    cde
    11    df
    42    dfg
    9     e  
    $ cat f2.sort
    23    abc
    8     df
    12    dfg
    24    gjr
    $ join -1 2 -2 2 -o 1.1,2.1,0 -a 1 -a 2 -e NA f1.sort f2.sort
    12 23 abc
    34 NA cde
    11 8 df
    42 12 dfg
    9 NA e
    NA 24 gjr
    $
    

    With process substitution, you could write:

    join -1 2 -2 2 -o 1.1,2.1,0 -a 1 -a 2 -e NA <(sort -k2b File1) <(sort -k2b File2)
    

    If you want the data formatted differently, use awk to post-process the output:

    $ join -1 2 -2 2 -o 1.1,2.1,0 -a 1 -a 2 -e NA f1.sort f2.sort |
    > awk '{ printf "%-5s %-5s %s\n", $1, $2, $3 }'
    12    23    abc
    34    NA    cde
    11    8     df
    42    12    dfg
    9     NA    e
    NA    24    gjr
    $
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two files. file1 has the data like belowing containing only one column.
I have two FASTA files: file1.fasta >foo ATCGGGG >bar CCCCCC file2.fasta >qux ATCGGAAA What
I have two txt files: File1.txt – contains list of src dir; and File2.txt
I have a bunch of files (with same name, say abc.txt) strewn all over
I have a directory /tmp/dir with two types of file names /tmp/dir/abc-something-server.log /tmp/dir/xyz-something-server.log ..
I have two files called file_utils.h and file_utils.cpp which contain some methods and variables
If I have two css files: File 1: .colorme { background-color:Red; } File 2:
I have two files: master/newsletter1/file.html master/newsletter2/file.html newsletter1/file.html has a lot of new changes that
I have two files client.php and server.php. The client file send a HTTP request
I have two files: public interface PrintService { void print(PrintDetails details); class PrintDetails {

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.