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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:20:41+00:00 2026-05-26T07:20:41+00:00

I have multiple tables in a file, such as: col1, col2, col3, col4 1,

  • 0

I have multiple “tables” in a file, such as:

col1, col2, col3, col4
1, 2, 3, 4
5, 6, 7, 8

col2, col3, col5
10, 11, 12
13, 14, 15

And I would like to collapse these 2 tables to:

col1, col2, col3, col4, col5
1   , 2   , 3   , 4   , 
5   , 6   , 7   , 8   , 
    , 10  , 11  ,     , 12
    , 13  , 14  ,     , 15

(Note: extra whitespace left just to make things easier to understand)

This would seem to require at least 2 passes, one to collect the full list of columns, and another one to create the output table. Is it possible to do this with awk? If not, what other tool would you recommend?

  • 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-26T07:20:42+00:00Added an answer on May 26, 2026 at 7:20 am

    The code assumes that the tables are separated by empty lines:

    awk -F', *' 'END {
      for (i = 0; ++i <= c;)
        printf "%s", (cols[i] (i < c ? OFS : RS))
      for (i = 0; ++i <= n;)
        for (j = 0; ++j <= c;)
          printf "%s", (vals[i, cols[j]] (j < c ? OFS : RS))    
      }
    !NF { 
      fnr = NR + 1; next 
      }
    NR == 1 || NR == fnr  {
     for (i = 0; ++i <= NF;) {
       _[$i]++ || cols[++c] = $i
       idx[i] = $i
       }
      next 
      }
    {  
      ++n; for (i = 0; ++i <= NF;)
             vals[n, idx[i]] = $i
       }' OFS=', ' tables
    

    If you have the tables in separate files:

    awk -F', *' 'END {
      for (i = 0; ++i <= c;)
        printf "%s", (cols[i] (i < c ? OFS : RS))
      for (i = 0; ++i <= n;)
        for (j = 0; ++j <= c;)
          printf "%s", (vals[i, cols[j]] (j < c ? OFS : RS))    
      }
    FNR == 1 {
     for (i = 0; ++i <= NF;) {
       _[$i]++ || cols[++c] = $i
       idx[i] = $i
       }
      next 
      }
    {  
      ++n; for (i = 0; ++i <= NF;)
             vals[n, idx[i]] = $i
       }' OFS=', ' file1 file2 [.. filen] 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple tables with an id component. I'd like to use LINQ to
I have multiple tables in a mysql database and I am trying to put
I have multiple tables that need to be merged into one. SELECT name, SUM(money)
I will have multiple tables used by different projects on the same mySql server.
I have multiple level parent-child tables(GrandParent, Parent and Child, all many to one relation,
I have a dataset with multiple tables. One table in particular has one record
I have a stored procedure that returns multiple tables. How can I execute and
I have an SQL database with multiple tables, and I am working on creating
I have a MySQL database with multiple tables, each of which have the same
I have a simple database with multiple tables. I can't figure out how to

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.