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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:38:36+00:00 2026-06-14T12:38:36+00:00

I have a tab delimited file, for which I need to concatenate selective columns

  • 0

I have a tab delimited file, for which I need to concatenate selective columns into a key-value pair. Let’s say my first three columns stay the same, but after that, I can expect any number of columns. All the columns after the third should concatenate into a key-value pair.

Here is my data set:

col1 col2 col3 col4 col5

1 2 3 4 5

Result

col1 col2 col3 col4|col5

1 2 3 col4 => 4 | col5 => 5

  • 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-14T12:38:38+00:00Added an answer on June 14, 2026 at 12:38 pm

    If you have the following input file:

    col1,col2,col3,col4,col5
    1,2,3,this,that
    2,3,4,some,more
    3,4,5,a,b
    

    Then pulling off the header, splitting and destructuring each line can be done with something like:

    header, *lines = File.readlines('var.txt')
    header = header.split(',')
    header[-1].chomp!
    rest_fields = header.slice(3..-1)
    recs = lines.map do |line|
      line.chomp!
      f1, f2, f3, *rest = line.split(',')
      pairs = {}
      rest_fields.each do |f|
        pairs[f] = rest.shift
      end
      [f1, f2, f3, pairs]
    end
    
    
    recs.each do |rec|
      puts rec.inspect
    end
    

    Running that gives:

    ["1", "2", "3", {"col4"=>"this", "col5"=>"that"}]
    ["2", "3", "4", {"col4"=>"some", "col5"=>"more"}]
    ["3", "4", "5", {"col4"=>"a", "col5"=>"b"}]
    

    Is this what you’re after?

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

Sidebar

Related Questions

I need to read test.TXT file(tab delimited) into MATLAB. TXT file have form: Datum
I have a tab delimited file with 5 columns and need to retrieve a
I have a tab delimited file which is being parsed and then inserted into
I have a tab delimited file in which starting from columns 10-25 some of
I have a tab delimited file, in which the first 10 columns are always
I have a tab delimited file which looks like this: CHROM <TAB> POS <TAB>
I have a text file with tab delimited data spread across 16 columns. I
I have a tab delimited file which looks like this: ABCA2 chr9 139021506 139043195
I have a large file named CHECKME which is tab delimited. There are 8
I have a tab-delimited text file which contains dagger characters ( † ). When

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.