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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:34:27+00:00 2026-05-27T04:34:27+00:00

I would like to combine entries from the second field from two files using

  • 0

I would like to combine entries from the second field from two files using awk, sed or similar.

File0:

string:data:moredata

File1:

string:random:moredata

If the first field, string in file0 has a matching entry in file1 then print

$random:$data

Selecting the fields seems trivial:

$ awk -F':' '{print $2}' filename

Need to match rows and print matching column $2

  • 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-27T04:34:28+00:00Added an answer on May 27, 2026 at 4:34 am

    How about this one –

    awk -F":" 'NR==FNR {x[$1] = $0; y[$1] = $2; next} ($1 in x) {print $2":"y[$1]}' file1 file2
    

    Execution:

    [jaypal~/Temp]$ cat file1
    string:data:moredata
    
    [jaypal~/Temp]$ cat file2
    string:random:moredata
    
    [jaypal~/Temp]$ awk -F":" 'NR==FNR {x[$1] = $0; y[$1] = $2; next} ($1 in x) {print $2":"y[$1]}' file1 file2
    random:data
    

    In this solution, we are loading the whole record of file1 in to the array indexed at column 1. We do a quick check in the next file to see if the column 1 is present. If it is then print statement is executed.

    Negative Test:

    [jaypal~/Temp]$ cat file1
    string:data:moredata
    man:woman:child
    
    [jaypal~/Temp]$ cat file2
    man:random:moredata
    string:woman:child
    
    [jaypal~/Temp]$ awk -F":" 'NR==FNR {x[$1] = $0; y[$1] = $2; next} ($1 in x) {print $2":"y[$1]}' file1 file2
    random:woman
    woman:data
    

    Just to add to the explanation, NR and FNR are awk’s in-built variables. NR gives line number and does not get reset to 0 when looped over two files. FNR on the contrary is also a line number that gets reset to 0 when second file starts. Thus this allows us to store the file 1 into the array because that action is only done when NR==FNR. As soon as this condition becomes false, it means the second file has started and next pattern action statement begins to execute.

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

Sidebar

Related Questions

I have two similar functions that I would like to combine so that I
I have two views that I would like to combine into one. The first
I have two regualar rexpressions I would like to combine into the one for
I would like to combine the functionality of two arguments to one. Currently -f
In javascript I would like to combine two objects into one. I got {
I have two queries which i would like to combine There are 4 tables
I have the following 2 arrays and would like to combine them. I'm more
I have some old C code that I would like to combine with some
In an asp.net application, I would like to combine the use of the Webclient
This is the sequel to this question . I would like to combine three

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.